本节知识点
使用指南
- 首先在仓库文件里我这里就是store.js文件 写
const mutations = {
add:function(state,n){state.score=state.score+n;},
del:function(state,n){state.score=state.score-n;}
}
import store from "@/vuex/store";
<div>
<button @click="$store.commit('add',5)">添加</button>
<button @click="$store.commit('del',5)">减少</button>
</div>
要是想在内部使用方法必须加this
methods:{
cun:function(index){
var stat1 = JSON.parse(window.localStorage.getItem("goods"))||[];
var obj = this.love[index];
stat1.push(obj);
var result2 = JSON.stringify(stat1);
window.localStorage.setItem('goods',result2);
this.$store.commit('add',obj);
}