简体   繁体   English

Vue 3 从父组件更新子组件的数据

[英]Updating data on child components from a parent component in Vue 3

I'm migrating an application from KnockoutJS to VueJS.我正在将应用程序从 KnockoutJS 迁移到 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.它类似于选项卡结构,其中只有 1 个项目可以处于活动状态,这是使用我已更改为 ref 的名为 isActive 的可观察对象控制的。 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.当用户单击某个项目时,我想遍历父级列表中的子组件,并在将单击的项目上的 isActive 设置为 true 之前将 isActive 值设置为 false。

I have been able to update the clicked item's isActive value but I can't see the isActive ref values on the parent.我已经能够更新被点击项目的 isActive 值,但我看不到父项目的 isActive ref 值。 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 演示

If you console out menuItem.isActive.value in our changeSelectionWizardView function you will find your isActive reactive value.如果您在我们的changeSelectionWizardView function 中控制台输出menuItem.isActive.value ,您会发现您的 isActive 反应值。

But as I can see from your code you pass a plain/unreactive object as a props to your SelectionMenuItem components.但正如我从您的代码中看到的那样,您将一个普通/无反应的 object 作为道具传递给您的SelectionMenuItem组件。 So when you update the isActive value in the parent component the child will not get the change.因此,当您更新父组件中的 isActive 值时,子组件不会得到更改。

One possible solution is, make the menuItems a reactive data.一种可能的解决方案是,使 menuItems 成为反应性数据。 Then pass the reactive menuItem as a props to the child components.然后将响应式 menuItem 作为道具传递给子组件。 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.在每次change发出事件调用后,您只需更新 menuItems 的 isActive 值,然后道具将反应性地将最新更新的数据传递给子组件。

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.最后,一个小建议,尝试使用选项 API 或组合 API,不要同时使用它们,这会使您的代码调试更加困难。

You can use ref to access the child component in the parent:您可以使用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>

Similarly, in the child component you may access the parent component using $parent .同样,在子组件中,您可以使用$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.这不是特别推荐,因为它将子组件耦合到父组件(意思是,没有它它不会真正正常工作,目标通常应该是创建可以在任何地方重用的不可知组件),并且通常会有使用道具和发射事件可能是一个更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM