Javascript 无法在Vuex中使用store.subscribe()方法
我只是尝试使用Javascript 无法在Vuex中使用store.subscribe()方法,javascript,typescript,vue.js,vuejs2,vuex,Javascript,Typescript,Vue.js,Vuejs2,Vuex,我只是尝试使用Vuex中的store.subscribe()方法。我不断得到以下错误,这似乎表明没有store.subscribe方法,与文档中的相反: 未捕获的类型错误:已导入的网页\u模块\u 2\u存储。a.state.subscribe不是一个函数 这是我的代码: app.js文件,我在其中初始化所有内容,注册我的组件等: 从“Vuex”导入Vuex; 从“./routes”导入路由器; 从“./store”导入存储; window.Vue=require('Vue'); const
Vuex
中的store.subscribe()
方法。我不断得到以下错误,这似乎表明没有store.subscribe方法,与文档中的相反:
未捕获的类型错误:已导入的网页\u模块\u 2\u存储。a.state.subscribe不是一个函数
这是我的代码:
app.js
文件,我在其中初始化所有内容,注册我的组件等:
从“Vuex”导入Vuex;
从“./routes”导入路由器;
从“./store”导入存储;
window.Vue=require('Vue');
const app=新的Vue({
el:“#应用程序”,
路由器,
商店:新Vuex.store(商店)
});
store.subscribe((变异,状态)=>{
console.log(mutation.type)
console.log(mutation.payload)
})
我认为发生这种情况是因为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)
})
我认为这是因为
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)
})
添加
app
对象,如app.store…
,因为您的store
在该级别不被称为Vuex store实例
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)
});
app.$store.subscribe((mutation, state) => {
console.log(mutation.type)
console.log(mutation.payload)
})
添加
app
对象,如app.store…
,因为您的store
在该级别不被称为Vuex store实例
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)
});
app.$store.subscribe((mutation, state) => {
console.log(mutation.type)
console.log(mutation.payload)
})