vuex code is as follows
export default {
data () {
return {
content: ''
};
},
methods: {
onKeyup (e) {
if (e.ctrlKey && e.keyCode === 13 && this.content.length) {
this.sendMessage(this.content);
this.content = '';
}
},
sendMessage (content) {
store.dispatch('sendMessage', content);
}
}
};
When executing onKeyup, it prompts Uncaught TypeError: this.sendMessage is not a function
. I haven’t figured out whether I made a mistake somewhere. . . If you can figure it out, please give me some advice. Thank you.
Correct answer to the 1st floor
Generally speaking, the writing method using vuex should be
sendMessage (content) {
}
Then register the sendMessage method in actions.
Remember not to forget to write mutations.js too
Have you registered
sendMessage
thisaction
?Can’t just sendMessage directly? Why do we need this.sendMessage?