从VUE 3中的父组件更新子组件的数据

发布于 2025-01-20 05:03:12 字数 466 浏览 4 评论 0原文

我正在将应用程序从淘汰赛迁移到vuejs。我很难将模板转换为组件。它类似于一个选项卡结构,其中只能使用一个可观察的称为iSACTIVE的可观察到的iSActive来控制的选项卡结构,我将我更改为ref。当用户单击一个项目时,我想循环通过父列表中的列表中的子组件,并将iSARTIVE值设置为false,然后将单击项目的iSActive设置为true。

我已经能够更新点击项目的ISACTIVE值,但是我看不到父母的iSActive Ref值。我如何从父母那里获取孩子组件及其数据?

我有一个示例,说明这里的代码是什么样子。 demo

I'm migrating an application from KnockoutJS to VueJS. I'm having difficulty translating a template to a component. It's similar to a tab structure where only 1 item can be active which was controlled using an observable called isActive that I've changed to a ref. When a user clicks on an item I want to loop through the child components in a list on the parent and set the isActive value to false before setting isActive on the clicked item to true.

I have been able to update the clicked item's isActive value but I can't see the isActive ref values on the parent. How can I get the child components and their data from the parent?

I have an example of what the code looks like here. DEMO

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

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

发布评论

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

评论(2

绝影如岚 2025-01-27 05:03:12

您可以使用 ref 访问父组件中的子组件:

<template>
    <div>
        <child-component ref="mychild" />
    </div>
</template>

<script>
export default {
    mounted() {
        // this.$refs.mychild will be the instance of the child component.
        // You can access all of its data and methods
    }
}
</script>

同样,在子组件中您可以使用 $parent 访问父组件。但这并不是特别推荐的,因为它将子组件耦合到父组件(这意味着,如果没有它,它就无法真正正常工作,目标通常应该是创建可以在任何地方重用的不可知组件),并且通常会出现使用 props 和发射事件可能是一个更好的解决方案。

You can use ref to access the child component in the parent:

<template>
    <div>
        <child-component ref="mychild" />
    </div>
</template>

<script>
export default {
    mounted() {
        // this.$refs.mychild will be the instance of the child component.
        // You can access all of its data and methods
    }
}
</script>

Similarly, in the child component you may access the parent component using $parent. This isn't particularly recommended though as it couples the child component to the parent (meaning, it won't really work properly without it, and the goal should usually be to create agnostic components that can be reused anywhere), and usually there will be a better solution possible using props and emitting events.

羅雙樹 2025-01-27 05:03:12

如果您在 changeSelectionWizardView 函数中控制台 menuItem.isActive.value,您将找到 isActive 反应值。

但正如我从您的代码中看到的,您将一个普通/非反应对象作为道具传递给您的 SelectionMenuItem 组件。因此,当您更新父组件中的 isActive 值时,子组件将不会收到更改。

一种可能的解决方案是,使 menuItems 成为反应数据。然后将响应式 menuItem 作为 props 传递给子组件。在每次 change 发出事件调用后,您只需更新 menuItems isActive 值,然后 props 就会将最新更新的数据反应性地传递给子组件。

我对您的代码进行了一些更改,现在我认为它的工作原理就像您在上面的问题中提到的那样。这是 link

最后,一个小建议,尝试使用选项 API 或组合 API,不要同时使用它们同时,它使你的代码调试更加困难。

If you console out menuItem.isActive.value in our changeSelectionWizardView function you will find your isActive reactive value.

But as I can see from your code you pass a plain/unreactive object as a props to your SelectionMenuItem components. So when you update the isActive value in the parent component the child will not get the change.

One possible solution is, make the menuItems a reactive data. Then pass the reactive menuItem as a props to the child components. And after each change emit event call you just update the menuItems isActive value and then the props will reactively pass the latest updated data to the child components.

I have made some changes in your code, Now I think it's working like what you mentioned in your question above.Here is the link

Lastly, one small suggestion, try to use either option API or composition API, don't use both of them at the same time, It makes your code debugging harder.

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