Home > Web Front-end > Vue.js > How to install vuex in vue.js

How to install vuex in vue.js

coldplay.xixi
Release: 2020-12-01 15:05:09
Original
2222 people have browsed it

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].

How to install vuex in vue.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"
  },
Copy after login

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:{}
})
Copy after login

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: &#39;<App/>&#39;
})
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template