VUE 3 / PINIA:在更改时如何从商店中获取更新值?

发布于 2025-02-13 11:55:11 字数 1452 浏览 1 评论 0原文

在Interia设置中使用VUE 3与PINIA。我可以在开发工具中看到PINIA,并且可以安装登录(启动)值。

我的组件设置为:

<Comment>
    <ChildComment>

在ChildComment中,

const toggleEdit = () => {
    commentStore.childEditing = !editMode.value

    editMode.value = !editMode.value;
};

当我触发此功能时,我具有此功能,在商店中的值更新:

”

&lt; comment&gt;组件中,我尝试使用诸如so之类的各种方法:

import { useStore } from "../../store/commentStore";
const commentStore = useStore();


// USING STATE DIRECTLY
// const isEditingChildComment = ref(commentStore.childEditing);
// const isEditingChildComment = reactive(commentStore.childEditing);
// const isEditingChildComment = computed(() => commentStore.childEditing);

// USING GETTER
// const isEditingChildComment = ref(commentStore.getChildEditing);
// const isEditingChildComment = reactive(commentStore.getChildEditing);
// const isEditingChildComment = computed(() => commentStore.getChildEditing);

但是iseditingChildComment在以上6个方案中永远不会更新,它仍然是错误的:

“

我是否缺少明显的东西?

Using Vue 3 with Pinia in an Interia setup. I can see Pinia in my Dev Tools and I can console log out the (starting) value.

I have a component set up like:

<Comment>
    <ChildComment>

In ChildComment, I have this function

const toggleEdit = () => {
    commentStore.childEditing = !editMode.value

    editMode.value = !editMode.value;
};

When I trigger this function, the value updates in the store:

store screenshot

In the <Comment> component, I have tried using various methods like so:

import { useStore } from "../../store/commentStore";
const commentStore = useStore();


// USING STATE DIRECTLY
// const isEditingChildComment = ref(commentStore.childEditing);
// const isEditingChildComment = reactive(commentStore.childEditing);
// const isEditingChildComment = computed(() => commentStore.childEditing);

// USING GETTER
// const isEditingChildComment = ref(commentStore.getChildEditing);
// const isEditingChildComment = reactive(commentStore.getChildEditing);
// const isEditingChildComment = computed(() => commentStore.getChildEditing);

But isEditingChildComment is never updated in any of the above 6 scenarios, it remains as false:

Component stuff

Am I missing something obvious?

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

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

发布评论

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

评论(2

靑春怀旧 2025-02-20 11:55:12

如果您使用计算属性,您需要返回值。例如:

const isEditingChildComment = computed(() => {
   return commentStore.childEditing);
}

如果您直接突变商店状态,也看起来也是如此。我认为您应该使用动作为此,以保证反应性的工作。

If you use a computed property, you need to return a value. For example:

const isEditingChildComment = computed(() => {
   return commentStore.childEditing);
}

It looks also if you are mutating the store state directly. I think you should use an Action for that, to guarantee the working of reactivity.

撩心不撩汉 2025-02-20 11:55:12

在文档中,这确实是微妙的,但是我认为commentStore的分配。将变量的儿童分配本质上是在取消结构的商店,因此失去了反应性。
您应该能够直接在组件中使用CommentStore.Childeding。它将具有反应性。

It's really subtle in the docs but I think that assignment of commentStore.childEditing to a variable is essentially de-structuring the store and hence it loses reactivity.
You should be able to use commentStore.childEditing directly in the component and it will be reactive.

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