vue.js安裝vuex的方法:首先進(jìn)入項(xiàng)目路徑后,在項(xiàng)目終端輸入相關(guān)代碼;然后在項(xiàng)目的package json文件中顯示vuex插件;接著在項(xiàng)目src目錄中新建store js;最后在【main.js】中應(yīng)用vuex即可。
本教程操作環(huán)境:windows7系統(tǒng)、Vue2.9.6版,該方法適用于所有品牌電腦。
vue.js安裝vuex的方法:
1、安裝:
進(jìn)入項(xiàng)目路徑后,在項(xiàng)目終端輸入:npm install vuex --save
或者 cnpm install vuex --save
2、安裝完成后,會(huì)在項(xiàng)目的package.json文件中顯示vuex插件,如下:
"dependencies": { "vue": "^2.5.2", "vue-router": "^3.0.1", "vuex": "^3.3.0" },
3、再項(xiàng)目src目錄中新建store.js,文件內(nèi)容如下:
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); export default new Vuex.Store({ //所有的數(shù)據(jù)都放在state中 state:{}, //操作數(shù)據(jù),唯一的通道是mutations mutations:{}, //actions,可以來做異步操作,然后提交給mutations,而后再對(duì)state(數(shù)據(jù))進(jìn)行操作 actions:{} })
4、在main.js中應(yīng)用vuex:
導(dǎo)入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' //導(dǎo)入store Vue.config.productionTip = false /* eslint-disable no-new */ new Vue({ el: '#app', router, store, //在new Vue中使用 components: { App }, template: '<App/>' })
相關(guān)免費(fèi)學(xué)習(xí)推薦:javascript(視頻)