重新渲染父组件来自子组件

huangapple go评论63阅读模式
英文:

How to re-render parent from child component

问题

如何在子组件不直接从父组件调用而是通过多层嵌套的方式,即从另一个在父组件下数层的子组件中调用时,重新渲染父组件?

英文:

How do I re-render a parent component from child component when the child component is not called directly from the parent component, but through several levels of nesting, that is, from another child component that is several levels below the parent?

答案1

得分: 2

你不应该手动重新渲染任何父组件。所有更新都应该在你的响应式数据变化时初始化。所以确保你的父组件依赖的数据在子组件的数据更新时也得到更新。
可以通过事件向父组件传递来实现。但是,在嵌套层级很深的情况下,当父/子组件紧密耦合在一起时(子组件不能存在于没有父组件的情况下),我会使用 provide/inject,或者一些全局/存储的响应式对象。
无论如何,没有明确的答案,因为没有提供代码。
也许首先在父组件中尝试使用 provide,在子组件中使用 inject

英文:

You shouldn't manually re-render any parent components. All updates should be initialized on your reactive data's changes. So make sure the data your parent component depends on is updated when the child component's data is updated.
It could be done with events popping up to the parent components.
BUT. With a deep level of nesting I use either provide/inject when parent/child components are tightly bound together (the child component couldn't exist without the parent) or some global/store reactive object.
Anyway there's no clear answer because there's no code provided.
Maybe first try provide in the parent and inject in the child.

huangapple
  • 本文由 发表于 2023年5月14日 21:31:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定