1.入口文件main.js
import Vue from 'vue'
import Layout from './layout'
import router from './router'
// 引入vuex
import store from './store/store
vue实例中
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store,
components: { Layout },
template: '<Layout/>'
})
store文件下创建store.js
store.js中设置
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const state = {
pjtnews: 0,
count: 1
}
const mutations = {
add(state) {
state.count += 1;
},
reduce(state) {
state.count -= 1;
}
}
export default new Vuex.Store({
state,
mutations
});
以上参考 :