安装配置vuex
- 终端输入:npm i vuex 进行安装
- 创建文件 src/store/index.js
- 在index.js文件中配置以下内容:
//引入Vue核心库 import Vue from 'vue' //引入Vuex import Vuex from 'vuex' //应用Vuex插件 Vue.use(Vuex) //准备actions对象——响应组件中用户的动作 const actions = {} //准备mutations对象——修改state中的数据 const mutations = {} //准备state对象——保存具体的数据 const state = {} //创建并暴露store export default new Vuex.Store({ actions, mutations, state })
- 在main.js中配置以下内容:
...... //引入store import store from './store' ...... //创建vm new Vue({ el:'#app', render: h => h(App), store })
- 此时在组件实例身上已经存在$store,已经可以调用: