Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

vuex Cannot read property 'dispatch' of undefined

vuex 使用this.$store.dispatch()改变vuex的值,
报错Cannot read property 'dispatch' of undefined问题

首先定义好且引入正确了,我在普通的组件中,调用
this.$store.dispatch('zipFileType','.doctype')能够正确把这个值改成‘.doctype’

然后我在一个vtip弹层中,也想调用这个方法,却报上面那个错了。这个vtip是一个鼠标经过才显示的弹层组件(vtip插件),跟这个会有原因吗?

代码是确的,就是在这个插件弹层里调用$store.dispatch()就不行,请问下是什么原因呢,又该如何解决呢?感激不尽


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

将store注入app应用一般是在main.js

import store from '@/store'
import App from '@/App.vue'

new Vue({
    store,
    render: h => h(App)
    //...other code
})

在这个实例以及它的后代组件中,就都被注入了$store.

但有些组件,并不是这个实例的后代,一般是一些弹窗之类的,在他们实例中,就无法获取到$store.

检查你的vtip插件,是否是单独new的vue实例.
如果是单独new的,
可以直接引入store,替换原来的this.$store

import store from "@/store"

export default {
// ...other code
    methods: {
        someMethod() {
            // -- this.$store.dispatch('xxx')
            store.dispatch('xxx')
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...