vue.js安裝vuex的方法:首先進入項目路徑后,在項目終端輸入相關代碼;然后在項目的package json文件中顯示vuex插件;接著在項目src目錄中新建store js;最后在【main.js】中應用vuex即可。
本教程操作環境:windows7系統、Vue2.9.6版,該方法適用于所有品牌電腦。
vue.js安裝vuex的方法:
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/>' })
相關免費學習推薦:javascript(視頻)