Appearance
useComponentInstance
▸ useComponentInstance(refName, instance?): Ref<null | ComponentInternalInstance>
通过 ref 名称获取组件实例
使用场景:在 ref 属性值为已知的字符串的情况下,更推荐使用官方的 访问模板引用 语法糖
在 ref 属性值为动态赋值的情况下,可使用该 Hooks 方法获取 ref 指向
参数
| Name | Type | Description |
|---|---|---|
refName | string | ref 名称 |
instance? | ComponentInternalInstance |
返回值
Ref<null | ComponentInternalInstance>
组件实例
示例
ts
const dialog = useComponentInstance(`dialog-${id}`)
<mp-dialog :ref="`dialog-${id}`" />