Laravel 此2.$dispatch不是一个函数

Laravel 此2.$dispatch不是一个函数,laravel,vue.js,vuejs2,vue-component,vuex,Laravel,Vue.js,Vuejs2,Vue Component,Vuex,我正在尝试像这样使用vue.js中的dispatch函数。 但是我在说这句话时出错了。2.$dispatch不是一个函数……就像你在屏幕截图上看到的那样 message.vue export default { data(){ return{ message:'', isLoading:false, } }, methods:{ addMessage(){

我正在尝试像这样使用vue.js中的dispatch函数。 但是我在说这句话时出错了。2.$dispatch不是一个函数……就像你在屏幕截图上看到的那样

message.vue

  export default {
    data(){
        return{
            message:'',
            isLoading:false,
        }
    },

    methods:{
        addMessage(){
            if(this.message !=''){
                this.sendData();
            } else{
                this.$fire({
                    title: "Error",
                    text: "Enter some text.",
                    type: "error",
                    timer: 3000
                }).then(r => {
                    console.log(r.value);
                });
                setTimeout(() => {
                    this.isLoading = false
                },700)
            }
        },
        sendData(){
            this.isLoading = true;
            this.$http.post('/add-message' , {message:this.message, id_rooms:this.$route.params.id_rooms}).then((response) => {
                console.log(response.json());
                if(response.body != 'Error'){
                    this.message = '';
                    this.$dispatch('new_message', response.json());
                } else{
                    this.isLoading = false;
                }
            }, (response) =>{

            });
        }
    }
}
 export default {
        components:{
            get_message:getMessage,
            add_message:addMessage,
        },
        data(){
            return{
                messages:[]
            }

        },
        events:{
            'new_message':function(data){
                this.messages.push(data);
            }
        }

    }
然后我就想这样把它弄出来

chat.vue

  export default {
    data(){
        return{
            message:'',
            isLoading:false,
        }
    },

    methods:{
        addMessage(){
            if(this.message !=''){
                this.sendData();
            } else{
                this.$fire({
                    title: "Error",
                    text: "Enter some text.",
                    type: "error",
                    timer: 3000
                }).then(r => {
                    console.log(r.value);
                });
                setTimeout(() => {
                    this.isLoading = false
                },700)
            }
        },
        sendData(){
            this.isLoading = true;
            this.$http.post('/add-message' , {message:this.message, id_rooms:this.$route.params.id_rooms}).then((response) => {
                console.log(response.json());
                if(response.body != 'Error'){
                    this.message = '';
                    this.$dispatch('new_message', response.json());
                } else{
                    this.isLoading = false;
                }
            }, (response) =>{

            });
        }
    }
}
 export default {
        components:{
            get_message:getMessage,
            add_message:addMessage,
        },
        data(){
            return{
                messages:[]
            }

        },
        events:{
            'new_message':function(data){
                this.messages.push(data);
            }
        }

    }
我在控制台中遇到了这个错误…有什么办法可以解决这个问题吗

更新 如果您的商店已在Vue注册,它似乎应该可以正常工作。如果您的
$dispatch
超出了承诺范围,您可以尝试将
上下文存储在另一个变量中

sendData(){
this.isLoading=true;
常数=this;
这是$http
.post('/add message',{message:this.message,id_rooms:this.$route.params.id_rooms})
。然后((响应)=>{
log(response.json());
if(response.body!=“Error”){
该消息=“”;
.dispatch('new_message',response.json());
}否则{
this.isLoading=false;
}
},(回应)=>{
});
}
或者只是
$dispatch

sendData(){
this.isLoading=true;
const$dispatch=此。$dispatch;
这是$http
.post('/add message',{message:this.message,id_rooms:this.$route.params.id_rooms})
。然后((响应)=>{
log(response.json());
if(response.body!=“Error”){
this.message='';
$dispatch('new_message',response.json());
}否则{
this.isLoading=false;
}
},(回应)=>{
});
}

在这里猜猜,但是试着打电话给这个

this.$store.dispatch('new_message',response.json());
或者,您的问题可能是范围问题(看到这是从承诺中调用的)

如果在promise处理程序
中有这样声明的函数,那么(函数(响应){this.$store.dispatch('new_message',response.json();})
可能是由于作用域的原因

相反,您可以尝试使用arrow函数

然后((响应)=>{
这个.store.dispatch('new_message',response.json());
})

this.$dispatch('new_message',response.json());您能否提供更多关于如何调用此的上下文信息?最常见的情况是,您使用的是
this
,但实际上它不是您期望的上下文(例如,您在事件处理程序中使用它,它是元素上下文而不是组件上下文)。我们不能确定,除非我们知道你是如何使用的that@Daniel我更新了我的代码您是否尝试过
this.$store.dispatch('new_message',response.json())?我不知道这是什么。$dispatch
做的,但我以前没有遇到过。@Daniel是的,我试过了…现在有一个错误app.js:1980 Uncaught(in promise)TypeError:无法读取未定义的属性'dispatch'