1.安装插件
npm i vuex@3 // 注意vue2用vuex3版本、vue3用vuex4版本
2.在main.js 中引入vuex
import store from './store' // 引入store
new Vue({
sotre,
render: h => h(App),
}).$mount('#app')
3.创建store文件夹
// store/index.js
import Vue form 'vue'
import Vuex form 'vuex'
Vue.use(Vuex)
const actions = {},
const mutations = {},
const state = {},
export default new Vuex.Store({
actions,
mutations,
state
})
注意:Vue.use(Vuex) 要在store 文件夹中,不能在main.js中