如何使用vuex级访问nuxt

发布于 2025-01-21 23:53:02 字数 1461 浏览 0 评论 0 原文

我正在使用NUXT构建应用程序。我第一次与Vuex一起玩。我遵循了许多教程来进行此设置,但是我遇到了访问商店的问题,我开始认为这可能与NUXT有关。

首先,我想使用商店控制一个Vuetify对话框。

store/index.ts

import { GetterTree, ActionContext, MutationTree } from 'vuex'


export type State = ReturnType<typeof state>

// state
export const state = () => ({
  recipeFormDialog: false,
})

// getters
export const getters: GetterTree<State, State> = {
  recipeFormDialog: (state: State) => {state.recipeFormDialog},
}

// mutations
export const mutations = {
  open(state: State): void {
    state.recipeFormDialog = true
  },
  close(state: State): void {
    state.recipeFormDialog = false
  },
}

// actions
export const actions = {
  openRecipeFormDialog(context: ActionContext<State, State>): void {
    context.commit('open')
  },
  closeRecipeFormDialog(context: ActionContext<State, State>): void {
    context.commit('close')
  },
}

pages/example.vue

<template>
  <div>
    {{recipeFormDialog}}
  </div>
</template>

<script lang='ts'>

import {Component, Prop, Vue} from 'vue-property-decorator';
import {Getter} from 'vuex-class';

@Component({})
export default class ExamplePage extends Vue {
  @Getter recipeFormDialog!: boolean;
}

问题是 recipeFormDialog 是未定义的,因此不会在页面上显示。我根本无法查看该值。我是否不正确地配置商店?

我真的很想坚持基于班级的组件和装饰器,因为我发现它比替代方案要干净得多。

事先感谢您的帮助

I am building an application using Nuxt. I am playing with vuex for the first time. I have followed a bunch of tutorials to get this set up, but I am running into issues accessing the store and I am starting to think it may be related to Nuxt.

To start, I'd like to control a Vuetify dialog using the store.

store/index.ts

import { GetterTree, ActionContext, MutationTree } from 'vuex'


export type State = ReturnType<typeof state>

// state
export const state = () => ({
  recipeFormDialog: false,
})

// getters
export const getters: GetterTree<State, State> = {
  recipeFormDialog: (state: State) => {state.recipeFormDialog},
}

// mutations
export const mutations = {
  open(state: State): void {
    state.recipeFormDialog = true
  },
  close(state: State): void {
    state.recipeFormDialog = false
  },
}

// actions
export const actions = {
  openRecipeFormDialog(context: ActionContext<State, State>): void {
    context.commit('open')
  },
  closeRecipeFormDialog(context: ActionContext<State, State>): void {
    context.commit('close')
  },
}

pages/example.vue

<template>
  <div>
    {{recipeFormDialog}}
  </div>
</template>

<script lang='ts'>

import {Component, Prop, Vue} from 'vue-property-decorator';
import {Getter} from 'vuex-class';

@Component({})
export default class ExamplePage extends Vue {
  @Getter recipeFormDialog!: boolean;
}

The problem is that recipeFormDialog is undefined and thus will not show on the page. I have not been able to view the value at all. Am I configuring the store improperly?

I would really like to stick with the class-based components and decorators because I find it to be much cleaner than the alternative.

Thanks in advance for the assistance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薯片软お妹 2025-01-28 23:53:02

对于其他观察此问题的人来说,“ Vue-Property-decorator”似乎取决于“ Vue-Class-Component”,这在VUE3中不支持,这解释了为什么我无法访问值

...在这里信息

For anyone else observing this issue, it seems that 'vue-property-decorator' depends on 'vue-class-component' which is not supported in vue3 which explains why I was unable to access the values... duh...

more info here
https://github.com/vuejs/rfcs/pull/17#issuecomment-494242121

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文