Answer the question
In order to leave comments, you need to log in
How to call a function from methods in created?
export default {
name: 'App',
data(){
return {
messages: [],
newMessage: null,
connection: null
};
},
created: function() {
this.connection = new WebSocket("ws://127.0.0.1:8000/ws/chat/")
this.connection.onmessage = function(event) {
let receivedMessage = JSON.parse(event.data).message;
this.receiveMessage(receivedMessage)
}
this.connection.onopen = function(event) {
return event
}
},
methods: {
sendMessage: function () {
this.connection.send('{"message":"'+this.newMessage+'"}');
},
receiveMessage(message) {
this.messages.push(message)
}
}
}
this.receiveMessage is not a function
Answer the question
In order to leave comments, you need to log in
Try like this:
created () {
this.connection = new WebSocket("ws://127.0.0.1:8000/ws/chat/")
this.connection.onmessage = event => {
let receivedMessage = JSON.parse(event.data).message;
this.receiveMessage(receivedMessage)
}
this.connection.onopen = function(event) {
return event
}
},
this.connection.onmessage = event => {
created: function() {
created () {
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question