Vuex

概念

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

何时使用

  • 多个组件需要共享数据时

搭建vuex环境

  1. 创建文件:src/store/index.js
  // 引入Vue核心库
  improt Vue from 'vue'
  // 引入Vuex
  improt Vuex from 'vuex'
  // 应用Vuex插件
  Vue.use(Vuex)

  // 准备actions对象   响应组件中用户的动作
  const actions = {}
  // 准备mutations对象 修改state中的数据
  const mustation = {}
  // 准备state对象  保存具体的数据
  const state = {}

  // 创建并暴露store
  export default new Vuex.Store({
    actions,
    mutations,
    state
  })
  1. 在main.js中创建vm时传入store配置项
posted @ 2022-01-29 23:52  HuangBingQuan  阅读(21)  评论(0编辑  收藏  举报