store.js的state中
const state = {
test:'',//把存的那个值的名字初始化一下
}
store.js的matution中
setTest(state,data){
state.test = data;//第二个参数随意起个名,这个data是state存的值
},
组件内存值
methods: {
fn() {
this.$store.commit('setTest',"zhangsan") //setValue存值随便起个名, zhangsan要存的值
}
}
组件内取值
//在计算属性中取值,js用直接this.value拿到值,html用直接test使用
computed: {
value() {
return this.$store.state.test;
}
},