Vue-如何在MutationAction中访问状态

时间:2020-07-13 22:05:48

标签: javascript typescript vue.js vuex

我无法访问@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

0 个答案:

没有答案