MGS*_*MGS 1 javascript typescript vue.js vuex vuejs2
我只是想store.subscribe()在Vuex. 我不断收到以下错误,这似乎表明没有 store.subscribe 方法,这与文档中的内容相反:
未捕获的类型错误:WEBPACK_IMPORTED_MODULE_2__store .a.state.subscribe 不是函数
这是我的代码:
app.js 文件,我在其中初始化所有内容,注册我的组件等:
import Vuex from 'vuex';
import router from './routes';
import store from './store';
window.Vue = require('vue');
const app = new Vue({
el: '#app',
router,
store: new Vuex.Store(store)
});
store.subscribe((mutation, state) => {
console.log(mutation.type)
console.log(mutation.payload)
})Run Code Online (Sandbox Code Playgroud)
我的store.js文件,作为我的集中Vuex存储。到目前为止,它一直在完美运行:
import router from './routes';
export default {
state: {
sample: {
}
},
mutations: {
sample(state){
}
},
getters: {
sample(state){
return state.sample
}
}
}Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题
我认为发生这种情况是因为store它仍然只是一个常规对象,而不是Vuex你调用它时的实例。您可以尝试以下操作:
import Vuex from 'vuex';
import router from './routes';
import store from './store';
window.Vue = require('vue');
const myStore = new Vuex.Store(store)
const app = new Vue({
el: '#app',
router,
store: myStore
});
myStore.subscribe((mutation, state) => {
console.log(mutation.type)
console.log(mutation.payload)
})
Run Code Online (Sandbox Code Playgroud)