我正在使用 Vue & Vuex 2 创建一个计数器。
当尝试访问 store 对象的 count 属性时,使用this.$store.state.count
,我得到一个Cannot read property 'state' of undefined
错误。
当我在里面创建商店实例而不是导入它时,该错误没有出现,并且一切正常。main.js
main.js
import Vue from 'vue'
import Vuex from 'Vuex'
import App from './App.vue'
import store from './store'
new Vue({
el: '#app',
store,
render: h => h(App)
})
store.js
import Vue from 'Vue'
import Vuex from 'Vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
count: 1
}
});
计数器.vue
export default {
name: 'counter',
template: `<span>{{ count }}</span>`,
computed: {
count () {
return this.$store.state.count
}
},
}
知道商店导入有什么问题吗?