vuex 简单的理解

本文介绍了Vuex中的关键概念:mutations用于同步更新状态,actions负责异步操作并触发mutations,getters则用于计算属性并缓存结果。示例展示了如何创建state、mutations、actions和getters,并解释了Vue中使用$store.commit和$store.dispatch的区别以及在模板中调用的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.创建对像
const state = {
        // 存放字典集,以key为健存储
        dictionaries: {
          root: {},
          page: {}
        },
        dictionary:'', //获取字典
        // 缓存请求结果,以url|uri为健存储
        allDictRequestCache: {}
}

2.mutations可以直接修改state存放方法
const mutations = {
    SET_CACHE: (state, cache) => {
        state.allDictRequestCache = cache
    }

3.actions是用来触发mutations的它无法直接改变state,它可以包含异步操作,它只能通过store.dispatch触发*通过commit去执行mutations的方法
const actions = {
  async pull (){}
}

export default {
    namespaced: true, //此属性是避免module属性与其他的module属性导出的state等内含属性重复报错所加
    state,
    mutations,
    actions
}

4. getters注册在全局的命名空间
 this.$store.getters['ec/dict/dictionary']

const getters = {

    dictionary (state) {}

}

Vue中this.$store.dispatch() 与 this.$store.commit()

1、commit: 同步操作
 * this.$store.commit('方法名',值)【存储】
 * this.$store.state.方法名【取值】
2、dispatch: 异步操作
 * this.$store.dispatch('方法名',值)【存储】
 * this.$store.getters.方法名【取值】

模板中vue中调用

import { mapActions, mapState } from "vuex";

...mapActions(["checkSatId"]),

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dogface07

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值