vuex的五个属性及用法

vuex是一个个专为 Vue.js 应用程序开发的状态管理工具,它采用集中式存储管理应用的所有组件的状态,而更改状态的唯一方法就是在mutaions里修改state,actions不能直接修改state

  1. state 数据存贮
  2. getter state的计算属性
  3. mutation 更改state中状态的逻辑 同步操作
  4. action 提交mutation 异步操作
  5. model 模块化

1.state 基本数据,存储变量

const store = new Vuex.Store({
state: {
    a: 'true',
    b: 2,
    c: false,
    d: null
  },
});

使用的时候

this.$store.state.a

commit:同步操作,写法: this.$store.commit(‘mutations方法名’,值)
回调函数就是我们实际进行状态更改的地方,并且它会接受 state 作为第一个参数,提交载荷作为第二个参数。


## 2. getter:从基本数据(state)派生的数据,相当于state的计算属性,具有返回值的方法

```javascript
getter: {
    a: state => state.a,
    b: function(state){
      return state.b * 2
  	}
  }

使用的时候

this.$store.getters.a

或者

import { mapGetters } from 'vuex';
computed: {
    ...mapGetters(['a'])
    }
    this.a
## 3.mutation:提交更新数据的方法,必须是同步的(如果需要异步使用action)。每个 mutation 都有一个字符串的 事件类型 (type) 和 一个 回调函数 (handler)```javascript
mutations: {
    a: (state, userId) => {
      state.userId = userId
    },
    b: (state, token) => {
      // console.log(token)
      state.token = token
    }
  },

4. action:和mutation的功能大致相同,不同之处在于 1) Action 提交的是 mutation,而不是直接变更状态。 2) Action 可以包含任意异步操作。

actions: { // {} 是es6中解构,把对象解构成属性
    a({ commit }, value) {
      commit('SET_USER', value)
    },
    }

dispatch:异步操作,写法: this.$store.dispatch(‘actions方法名’,值)

5. modules:模块化vuex,可以让每一个模块拥有自己的state、mutation、action、getters,使得结构非常清晰,方便管理如下图

在这里插入图片描述

同步操作

在这里插入图片描述

  • 14
    点赞
  • 107
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vuex 是一个专门为 Vue.js 应用程序开发的状态管理库,用于管理组件之间共享的状态。Vuex 有五个核心属性:state、mutations、actions、getters 和 modules。 1. state:state 是一个存储数据的对象,用于存储应用程序中的状态。可以通过 this.$store.state.xxx 访问该属性的值。 2. mutations:mutations 是用于修改 state 中的数据的方法,它们必须是同步函数。可以通过 this.$store.commit('mutationName') 调用 mutations 中的方法。 3. actions:actions 用于处理异步操作和异步任务,可以通过 this.$store.dispatch('actionName') 调用 actions 中的方法。actions 中可以提交 mutations 来修改 state 中的数据。 4. getters:getters 用于访问 state 中的数据并进行计算,类似于 Vue.js 中的计算属性。可以通过 this.$store.getters.getterName 访问 getters 中的方法。 5. modules:modules 允许将 store 分割成模块,每个模块拥有自己的 state、mutations、actions 和 getters。可以通过 this.$store.state.moduleName.xxx 访问模块中的 state 属性。 使用方法: 1. 安装 Vuex:npm install vuex --save 2. 创建 store.js 文件,定义 state、mutations、actions、getters 和 modules 等属性。 3. 在 Vue 组件中引入 Vuex 并注册 store。 4. 在 Vue 组件中通过 this.$store.state.xxx、this.$store.commit('mutationName')、this.$store.dispatch('actionName') 和 this.$store.getters.getterName 访问和修改状态。 示例代码: store.js 文件: ``` import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: { count: 0 }, mutations: { increment (state) { state.count++ } }, actions: { increment (context) { context.commit('increment') } }, getters: { getCount: state => { return state.count } }, modules: { moduleA: { state: {}, mutations: {}, actions: {}, getters: {} }, moduleB: { state: {}, mutations: {}, actions: {}, getters: {} } } }) export default store ``` App.vue 文件: ``` <template> <div> <p>Count: {{ count }}</p> <button @click="increment">Increment</button> <p>GetCount: {{ getCount }}</p> </div> </template> <script> export default { computed: { count () { return this.$store.state.count }, getCount () { return this.$store.getters.getCount } }, methods: { increment () { this.$store.commit('increment') this.$store.dispatch('increment') } } } </script> ``` 以上是一个简单的 Vuex 示例,可以通过按钮点击来增加 count 的值,并通过 getters 获取 count 的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值