私はvuejsとvuexを使ってプロジェクトを開発するのに苦労していますが、コンポーネントのthis.$store.state.count
ではうまくいきません。どうして?
マイ設定:
"vuex": "^2.0.0"
store.js:
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
count: 12
},
mutations: {
increment (state) {
state.count++
}
},
strict: true
})
main.js:
import store from './vuex/store'
import Vue from 'vue'
new Vue({
store,
.
.
.
}).$mount('#app')
component.js:
<script>
export default {
name: 'landing-page',
created:() => {
console.log('status2')
console.log(this.$store.state.count)
}
}
</script>
エラー:
Uncaught TypeError: Cannot read property '$store' of undefined
エラーは '$のstore'が定義されていないことを意味するものではありません。慎重に読む。 「これ」は定義されていないと言います。 – Xufox
ああ、そうです。 'this'は未定義です。なぜここで 'this 'を使うことができないのですか? – steve
コンポーネント内の 'this'は' data'を参照します。 – joaumg