types(@vant/use): useRelation support InjectionKey (#9015)

This commit is contained in:
neverland 2021-07-12 20:40:30 +08:00 committed by GitHub
parent c2737446a6
commit f0a5931b6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 5 deletions

View File

@ -3,6 +3,7 @@ import {
isVNode,
provide,
reactive,
InjectionKey,
getCurrentInstance,
VNodeNormalizedChildren,
ComponentPublicInstance,
@ -57,13 +58,15 @@ export function sortChildren(
}
export function useChildren<
Child extends ComponentPublicInstance = ComponentPublicInstance
>(key: string | symbol) {
// eslint-disable-next-line
Child extends ComponentPublicInstance = ComponentPublicInstance<{}, any>,
ProvideValue = never
>(key: InjectionKey<ProvideValue>) {
const publicChildren: Child[] = reactive([]);
const internalChildren: ComponentInternalInstance[] = reactive([]);
const parent = getCurrentInstance()!;
const linkChildren = (value?: any) => {
const linkChildren = (value?: ProvideValue) => {
const link = (child: ComponentInternalInstance) => {
if (child.proxy) {
internalChildren.push(child);

View File

@ -3,6 +3,7 @@ import {
inject,
computed,
onUnmounted,
InjectionKey,
getCurrentInstance,
ComponentPublicInstance,
ComponentInternalInstance,
@ -15,8 +16,8 @@ type ParentProvide<T> = T & {
internalChildren: ComponentInternalInstance[];
};
export function useParent<T>(key: string | symbol) {
const parent = inject<ParentProvide<T> | null>(key, null);
export function useParent<T>(key: InjectionKey<ParentProvide<T>>) {
const parent = inject(key, null);
if (parent) {
const instance = getCurrentInstance()!;