counterというstoreモジュールで定義したcountUp()という関数が定義されていないと出てしまい、
上手くカウントアップできません。原因はどこでしょうか?
storeを別ファイルに分割するのは初めてなのでそこで上手くいっていないのだと思うのですが。。。
作っているのは単純なカウントアップのアプリです。
エラー詳細(ブラウザコンソール)
shell
1[Vue warn]: Property or method "countUp" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties. 2 3found in 4 5---> <Btn> at src/components/Btn.vue 6 <Counter> at src/components/Counter.vue 7 <VApp> 8 <App> at src/App.vue 9 <Root>
shell
1./src/ 2├── App.vue 3├── assets 4│ └── logo.png 5├── components 6│ ├── Btn.vue 7│ ├── Counter.vue 8│ └── Lbl.vue 9├── main.js 10├── router 11│ └── index.js 12└── store 13 ├── index.js 14 └── modules 15 └── counter.js
store/index.js
js
1// store/index.js 2import Vue from 'vue' 3import Vuex from 'vuex' 4import {counter} from './modules/counter' 5 6Vue.use(Vuex) 7 8export default new Vuex.Store({ 9 modules: { 10 counter 11 } 12})
store/modules/counter.js
js
1export const counter = { 2 namespaced: true, 3 state: { 4 count: 0 5 }, 6 mutations: { 7 countUp (state) { 8 state.count += 1 9 } 10 }, 11 actions: { 12 countUp () { 13 this.commit('countUp') 14 } 15 } 16}
components/Btn.vue
vue
1<template> 2 <input 3 type="button" 4 value="+1" 5 @click="countUp()" 6 /> 7</template> 8 9<script> 10import {mapActions} from 'vuex' 11 12export default { 13 name: 'Btn', 14 data () { 15 return { 16 } 17 }, 18 computed: { 19 counter: function () { 20 return this.$store.state.counter 21 } 22 }, 23 methods: { 24 ...mapActions([ 25 'counter/countUp' 26 ]) 27 } 28} 29</script> 30 31<style> 32 33</style>
components/Lbl.vue
vue
1<template> 2 <p>{{count}}</p> 3</template> 4 5<script> 6 7import {mapState} from 'vuex' 8 9export default { 10 name: 'Lbl', 11 computed: { 12 ...mapState({ 13 count: state => state.counter.count 14 }) 15 } 16} 17</script> 18 19<style> 20 21</style>
components/Counter.vue
vue
1<template> 2 <div> 3 <p>counter</p> 4 <btn /> 5 <lbl /> 6 </div> 7</template> 8 9<script> 10import Btn from './Btn' 11import Lbl from './Lbl' 12export default { 13 name: 'Counter', 14 components: { 15 Btn, 16 Lbl 17 } 18} 19</script> 20 21<style> 22</style>

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/23 01:40
2019/01/23 01:50