小程序初始提交
This commit is contained in:
22
cool-unix/cool/hooks/parent.ts
Normal file
22
cool-unix/cool/hooks/parent.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { getCurrentInstance } from "vue";
|
||||
|
||||
/**
|
||||
* 获取父组件
|
||||
* @param name 组件名称
|
||||
* @example useParent<ClFormComponentPublicInstance>("cl-form")
|
||||
* @returns 父组件
|
||||
*/
|
||||
export function useParent<T>(name: string): T | null {
|
||||
const { proxy } = getCurrentInstance()!;
|
||||
|
||||
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