1.安裝
$ npm install --save vuex
$ yarn add vuex
2. 創(chuàng)建store.js
3. 在store.js 中引入vue 以及vuex
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
4. 創(chuàng)建 store
const store = new Vue.Store({
//配置
})
5. 導(dǎo)出 store
export default store
6. 在main.js中導(dǎo)入store并且寫在new Vue中
import store from '路徑'
new Vue ({
store
})
2. 輔助函數(shù)一共有四個(gè)
mapState mapGetters mapMutations mapActions
這四個(gè)函數(shù)實(shí)現(xiàn)不了新的功能,只能讓代碼更簡潔
(降低代碼耦合)
輔助函數(shù)只能在Vue-cli中使用