javascript - Why does the following js code report a function not found error?
过去多啦不再A梦
过去多啦不再A梦 2017-06-30 09:53:26
0
3
696

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.

过去多啦不再A梦
过去多啦不再A梦

reply all(3)
女神的闺蜜爱上我

Correct answer to the 1st floor
Generally speaking, the writing method using vuex should be
sendMessage (content) {

this.$store.dispatch('sendMessage', content);

}

Then register the sendMessage method in actions.
Remember not to forget to write mutations.js too

学霸

Have you registered sendMessagethisaction?

女神的闺蜜爱上我

Can’t just sendMessage directly? Why do we need this.sendMessage?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template