Vuex- Action的 { commit }

没有检索到摘要

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

Vuex- Action的 { commit }

  Vuex 中 使用 Action 处理异步请求时,常规写法如下:

   getMenuAction:(context) =>{
            context.commit('SET_MENU_LIST',['承保2','核保2'])
        }
    }

  我们也可以使用如下简化写法,如下:

   actions:{
        getMenuAction:({commit}) =>{
            commit('SET_MENU_LIST',['承保2','核保2'])
        }
    }

  这是一种怎么的写法呢?

  其实这是 ES6 中一种被称为  变量解构赋值  新的语法知识,可参考我的另外一篇文章  变量解构赋值。

     我们可以看看  Vuex 中 注册 Action的源码如下:

  

  

  常规写法中的 context = {   dispatch: local.dispatch,
                     commit: local.commit,
                getters: local.getters,
                state: local.state,
                rootGetters: store.getters,
                rootState: store.state                         }

 

  使用 变量解构赋值后 { commit } 的 commit= context.commit 了。

### vuex-class 装饰器库使用指南 vuex-class 是一个用于简化 Vuex 状态管理的装饰器库,它通过 TypeScript 的类装饰器机制与 Vue 组件结合,使状态管理和组件交互更加直观和高效。以下是详细的使用指南: #### 1. 安装依赖 确保项目中已经集成了 Vue 和 Vuex,然后安装 `vuex-class` 和相关依赖[^3]: ```bash npm install vuex-class vue-class-component class-validator # 或者 yarn add vuex-class vue-class-component class-validator ``` #### 2. 创建 Vuex 模块 使用 `vuex-class` 时,通常需要定义模块化的 Vuex Store。以下是一个简单的 Vuex 模块示例[^2]: ```typescript import { Module, VuexModule, Mutation, Action } from 'vuex-module-decorators'; @Module({ namespaced: true, name: 'example' }) class ExampleModule extends VuexModule { public count = 0; @Mutation increment(value: number) { this.count += value; } @Action({ rawError: true }) async incrementAsync(value: number) { await new Promise((resolve) => setTimeout(resolve, 1000)); this.context.commit('increment', value); } } ``` #### 3. 注册模块到 Vuex Store 将模块注册到 Vuex Store 中: ```typescript import { Store } from 'vuex'; import { ExampleModule } from './modules/example'; const store = new Store({ modules: { example: ExampleModule, }, }); ``` #### 4. 在 Vue 组件中使用装饰器 通过 `vuex-class` 提供的装饰器,可以直接在 Vue 组件中访问 Vuex 的状态、getter、mutation 和 action。以下是一个示例[^1]: ```typescript import { Component, Vue } from 'vue-property-decorator'; import { State, Getter, Mutation, Action } from 'vuex-class'; @Component export default class App extends Vue { @State((state) => state.example.count) count!: number; @Getter('example/someGetter') someGetter!: any; @Mutation('example/increment') increment!: (value: number) => void; @Action('example/incrementAsync') incrementAsync!: (value: number) => Promise<void>; mounted() { this.increment(1); // 调用 mutation this.incrementAsync(2).then(() => { console.log('异步操作完成'); }); } } ``` #### 5. 使用 TypeScript 类型支持 `vuex-class` 支持 TypeScript 类型推断,确保代码的安全性和可维护性。例如,在定义模块时可以明确指定类型: ```typescript import { Module, VuexModule, Mutation, Action } from 'vuex-module-decorators'; interface IExampleState { count: number; } @Module({ namespaced: true, name: 'example' }) class ExampleModule extends VuexModule implements IExampleState { public count = 0; @Mutation increment(value: number): void { this.count += value; } @Action async incrementAsync(value: number): Promise<void> { await new Promise((resolve) => setTimeout(resolve, 1000)); this.context.commit('increment', value); } } ``` #### 6. 注意事项 - 确保 Vuex Store 和模块的命名空间正确配置,尤其是在多模块场景下。 - 使用 `vuex-class` 时,推荐结合 `vue-class-component` 和 TypeScript,以充分利用类装饰器的功能[^1]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值