How to install vuex with vue.js: first enter the project path, enter the relevant code in the project terminal; then display the vuex plug-in in the project's package json file; then create a new store js in the project src directory; finally Just apply vuex in [main.js].
The operating environment of this tutorial: Windows 7 system, Vue version 2.9.6. This method is suitable for all brands of computers.
vue.js How to install vuex:
1. Installation:
After entering the project path, enter: npm in the project terminal install vuex --save
or cnpm install vuex --save
2. After the installation is completed, the vuex plug-in will be displayed in the project's package.json file, as follows:
"dependencies": { "vue": "^2.5.2", "vue-router": "^3.0.1", "vuex": "^3.3.0" },
3. Create a new store.js in the project src directory. The file content is as follows:
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. Apply vuex in main.js:
Import store, and then new Used in Vue, the code is as follows:
// 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/>' })
Related free learning recommendations: javascript (video)
The above is the detailed content of How to install vuex in vue.js. For more information, please follow other related articles on the PHP Chinese website!