You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nuxt项目使用vuex-class访问Vuex Getters返回undefined问题咨询

问题根因

获取到undefined是getter定义漏写返回值导致的,和Nuxt框架特性、类组件装饰器写法无关。
你在store/index.ts中定义的getter如下:

recipeFormDialog: (state: State) => {state.recipeFormDialog},

箭头函数使用大括号包裹函数体时,必须手动添加return才会返回执行结果,当前写法没有返回任何值,所以getter默认返回undefined。

修复步骤

修正getter返回值

两种修改方式二选一即可:

  • 箭头函数简写(去掉大括号,自动返回表达式结果)
// getters
export const getters: GetterTree<State, State> = {
  recipeFormDialog: (state: State) => state.recipeFormDialog,
}
  • 保留大括号,手动写return
// getters
export const getters: GetterTree<State, State> = {
  recipeFormDialog: (state: State) => {
    return state.recipeFormDialog
  },
}

现有装饰器写法验证

你当前使用vuex-class的@Getter装饰器配合类组件的写法完全符合规范,不需要调整。后续如果要调用弹窗开关的action,直接引入对应装饰器即可,参考示例:

<script lang='ts'>
import {Component, Vue} from 'vue-property-decorator';
import {Getter, Action} from 'vuex-class';

@Component({})
export default class ExamplePage extends Vue {
  @Getter recipeFormDialog!: boolean;
  @Action openRecipeFormDialog!: () => void;
  @Action closeRecipeFormDialog!: () => void;

  // 业务逻辑里直接调用 this.openRecipeFormDialog() 即可打开弹窗
}
</script>

补充说明:你当前的store目录结构、index.ts配置完全符合Nuxt2的自动注册约定,不需要额外做store注册操作。

内容的提问来源于stack exchange,提问作者Austin Hallett

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.06 16:15:43