Vuex的配置环境

vuex

概念

在vue中实现集中式状态(数据)管理的一个Vue插件,对vue应用中多个组件的共享状态进行集中式的管理(读/写),也是一种组件间通信的方式,且适用于任意组件间通信。

何时使用?

多个组件需要共享数据时

搭建vuex环境

创建:src/store/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 中创建vm时传入store配置项

...
//引入store
import store from './store'
...
//创建vm
new vue({
    el: '#app ' ,
    render: h => h(App),
    store
})
posted @ 2022-03-10 19:40  苏槿年  阅读(31)  评论(0编辑  收藏  举报