search

Home  >  Q&A  >  body text

javascript - Why does the following js code report a function not found error?

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梦2794 days ago701

reply all(3)I'll reply

  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-30 09:55:26

    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

    reply
    0
  • 欧阳克

    欧阳克2017-06-30 09:55:26

    Have you registered sendMessagethisaction?

    reply
    0
  • 女神的闺蜜爱上我

    女神的闺蜜爱上我2017-06-30 09:55:26

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

    reply
    0
  • Cancelreply