Vuex搭建环境
一、下载vuex
注意版本
vue2对应
npm i vuex@3
vue3对应
npm i vuex@4
二、创建文件 src/store/index.html
import Vue from "vue"; 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配置项
// 引入store import store from "./store"; new Vue({ el:'#app', render: h => h(App), store })