VUE 3 / PINIA:在更改时如何从商店中获取更新值?
在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:
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:
Am I missing something obvious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用计算属性,您需要返回值。例如:
如果您直接突变商店状态,也看起来也是如此。我认为您应该使用动作为此,以保证反应性的工作。
If you use a computed property, you need to return a value. For example:
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.
在文档中,这确实是微妙的,但是我认为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.