修改vuex中的值方法总结
在store文件夹下建立index.js
import Vue from "vue"; import Vuex from "vuex"; import VuexPersistence from "vuex-persist"; Vue.use(Vuex); const vuexLocal = new VuexPersistence({ storage: window.localStorage, }); export default new Vuex.Store({ state: { count:20 }, mutations: { updateCount(){ this.state.count=this.state.count+1; }, updateCounttwo(state,val){ this.state.count=this.state.count+val; } }, actions:{ updateCountActions(){ setTimeout(()=>{this.state.count=this.state.count+2; },1000) }, updateCountActions({ commit},data){ commit("updateCounttwo", data); } }, plugins: [vuexLocal.plugin], });
在vue中调用方法修改参数
1 import { mapActions, mapState } from "vuex"; 2 export default { 3 name:"RetrievalHeader", 4 methods:{ 5 ...mapActions(["updateCountActions"]), 6 submit2(){ 7 this.$store.commit('updateCount');//方法一 8 this.$store.dispatch("updateCountActions")//方法二 9 this.updateCountActions(5);//方法三 10 } 11 }, 12 } 13 </script>