1.安裝:
進入項目路徑后,在項目終端輸入:npm install vuex --save 或者 cnpm install vuex --save
2.安裝完成后,會在項目的package.json文件中顯示vuex插件,如下:
"dependencies": { "vue": "^2.5.2", "vue-router": "^3.0.1", "vuex": "^3.3.0" },
3.再項目src目錄中新建store.js,文件內容如下:
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); export default new Vuex.Store({ //所有的數據都放在state中 state:{}, //操作數據,唯一的通道是mutations mutations:{}, //actions,可以來做異步操作,然后提交給mutations,而后再對state(數據)進行操作 actions:{} })
4.在main.js中應用vuex:
導入store,然后再new Vue中使用,代碼如下:
// The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias. import Vue from 'vue' import App from './App' import router from './router' import store from './store' //導入store Vue.config.productionTip = false /* eslint-disable no-new */ new Vue({ el: '#app', router, store, //在new Vue中使用 components: { App }, template: '<App/>' })
https://blog.csdn.net/qq_41999617/article/details/84635325
https://www.cnblogs.com/songrimin/p/7815850.html