【问题标题】:Vue - how to access the state within MutationActionVue - 如何访问 MutationAction 中的状态
【发布时间】:2020-11-03 04:32:24
【问题描述】:

我无法访问@MutationAction中的状态

我正在使用:

Nuxt.js v2.13.3"vuex-module-decorators": "^0.17.0"

import { Module, VuexModule, MutationAction } from 'vuex-module-decorators'

@Module({
  name: 'counter',
  stateFactory: true,
  namespaced: true
})
export default class Auth extends VuexModule {
  public counter: number = 5

  @MutationAction({ mutate: ['counter'] })
  async set() {
    console.log(this.counter)
    return { counter }
  }
}

输出:undefined


这样就可以了

@MutationAction({ mutate: ['counter'] })
async set() {
  console.log((this.state as any).counter)
  return { counter }
}

输出:5


但如果我在阅读之前设置了一个值,一切都会顺利

@MutationAction({ mutate: ['counter'] })
async set() {
  this.counter = '20'
  console.log(this.counter)
  return { counter }
}

输出:20

有人可以帮我完成这项工作吗? this.counter

【问题讨论】:

    标签: javascript typescript vue.js vuex


    【解决方案1】:

    返回值被存储

    @MutationAction({ mutate: ['counter'] })
    async set(val) {
      return { counter: val }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-09-15
      • 2023-03-23
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 2017-07-25
      • 1970-01-01
      相关资源
      最近更新 更多