添加 cl-form 组件
This commit is contained in:
@@ -1,22 +1,22 @@
|
||||
import { getCurrentInstance } from "vue";
|
||||
|
||||
/**
|
||||
* 获取父组件实例
|
||||
*
|
||||
* 用于在子组件中获取父组件实例,以便访问父组件的属性和方法
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* // 在子组件中使用
|
||||
* const parent = useParent<ParentType>();
|
||||
* // 访问父组件属性
|
||||
* console.log(parent.someProperty);
|
||||
* ```
|
||||
*
|
||||
* @template T 父组件实例的类型
|
||||
* @returns {T} 返回父组件实例
|
||||
* 获取父组件
|
||||
* @param name 组件名称
|
||||
* @example useParent<ClFormComponentPublicInstance>("cl-form")
|
||||
* @returns 父组件
|
||||
*/
|
||||
export function useParent<T>(): T {
|
||||
export function useParent<T>(name: string): T | null {
|
||||
const { proxy } = getCurrentInstance()!;
|
||||
return proxy?.$parent as T;
|
||||
|
||||
let p = proxy?.$parent;
|
||||
|
||||
while (p != null) {
|
||||
if (p.$options.name == name) {
|
||||
return p as T | null;
|
||||
}
|
||||
p = p.$parent;
|
||||
}
|
||||
|
||||
return p as T | null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user