1.store/index.js文件
import Vue from "vue";
import Vuex from "vuex"
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
pageTitle: '',
echartsImg:''
},
mutations: {
getTitle(state, val) {
state.pageTitle = val
},
getImg(state, val) {
state.echartsImg = val
}
}
})
export default store
2.main.js引入
import store from '@/store/index.js' Vue.prototype.$store=store
3.使用
- 调用state中数据 this.$store.state.data;
- 调用 mutations中方法 同步方法 this.$store.commit('saveData',this.data)
- 调用actions中的方法 异步方法,用于调用mutations中方法 this.$store.dispatch('getData', this.data);