Vue3 中没有了$parent 和 $children,如何获取父子组件的属性

2,279 阅读1分钟

使用vue3实现findComponentUpward()和findComponentsDownward()

在调用时,需要使用getCurrentInstance()来获取对应的上下文,并将context传给第二个参数_context,将要查找的组件名称以字符串形式传给wantFind

const context = getCurrentInstance()

子组件向上查找父组件

默认返回用来判断是否存在,如需要父组件实例可将返回值修改为_contextcarbon.png

父组件向下查找子组件

carbon (1).png