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
}
});
Counter.vue
export default {
name: 'counter',
template: `<span>{{ count }}</span>`,
computed: {
count () {
return this.$store.state.count
}
},
}
ストアのインポートの何が問題なのか考えていますか?