使用@click 事件添加数字但不起作用。
计数器.vue
<script>
import { mapState, mapMutations } from "vuex"
export default {
data() {
return{
//payload
value: 1,
}
},
computed: {
...mapState(["counter"])
},
method: {
...mapMutations(["addToCounter"])
}
}
</script>
在 main.js 上
import { createApp } from 'vue'
import { createStore } from "vuex";
import App from './App.vue';
const store = createStore({
state() {
return {
counter: 11,
}
},
mutations: {
addToCounter(state, payload){
state.counter = state.counter + payload;
}
}
})
//connecting store to the application
const app = createApp(App)
//using the store that is created
app.use(store)
app.mount('#app')
没有错误,但 addToCounter 不起作用,而在触发 @click 时应该将值添加到计数器。