用vuex来实现数据共享,已经对vuex进行了配置,并且能够在后台输出共享值能够变化,但是无论用插值语法还是计算属性还是直接数据赋值,vue就是检测不到共享数据的变化
第一个代码(index.js也就是配置vuex)
//这个文件用来创建vuex中的核心store
import Vue from 'vue'
//引入vuex
import Vuex from 'vuex'
//准备actions----用来响应组件中的动作,中间商
const actions={
jia(context,value){
//context里面放着很多对象,有commit和dispatch等,value就是传进来的数据
context.commit('JIA',value)
//这个JIA是用来传给mutations,因为mutations是真正处理数据的
}
}
// 准备mutations-----用来操作数据(state )
const mutations={
JIA(state,value){
//这个state就是流程图的mutations的下一步state
state.sum+=value
// console.log(state);
}
}
//准备state-----用来存储数据
const state={
sum:0//和
}
//使用插件,有了vuex那么就有了store对象
Vue.use(Vuex)
//暴露store
//创建store
export default new Vuex.Store(
{
actions,
mutations,
state
}
)
第二个代码(MainComponent组件,用来修改共享数据)
<template>
<div class="big_box">
<h1>当前求和为:{{ this.$store.state.sum}}</h1>
<select v-model.number="n">
<option :value="1">1</option>
<option :value="2">2</option>
<option :value="3">3</option>
</select>
<button @click="add">+</button>
<button @click="dec">-</button>
<button @click="add_ood">当前求和为奇数再加</button>
<button @click="wait">等一等再加</button>
</div>
</template>
<script>
export default {
name: 'MainComponent',
data() {
return {
n:1,//用户输入的数字
watch_data: this.$store.state.sum
}
},
methods: {
add(){
this.$store.dispatch('jia',this.n)
this.watch_data=this.$store.state.sum
console.log(1111111,);
},
// dec(){
// },
// add_ood(){
// if (this.n%2) {
// }
// },
// wait(){
// setTimeout(()=>{
// },1000)
// }
},
computed: {
watchdata(){
console.log(this.$store.state.sum);
return this.$store.state.sum
}
},
}
</script>
<style scoped>
button{
margin-left: 10px;
}
</style>
在MainComponent组件里是为了修改index.js里面的sum值,this.$store.state.sum可以在后台输出数值变化,但是无论用什么方法渲染到网页上就不行,页面如下

而且报错
Error in nextTick: "TypeError: Cannot read properties of undefined (reading '_wrapper')",不知道错误是什么,希望得到解答,谢谢各位