/*! For license information please see 6289.bc713ea7.js.LICENSE.txt */ (self.webpackChunk=self.webpackChunk||[]).push([["6289"],{53301:function(s,n,a){"use strict";a.r(n);var t=a("80681");let e=["innerHTML"];n.default={setup:()=>({html:""}),render:()=>((0,t.wg)(),(0,t.iD)("div",{class:"van-doc-markdown-body",innerHTML:'

useRelation

\n

Intro

\n

Establish the association relationship between parent and child components, perform data communication and method invocation, based on provide and inject implementation.

\n

Usage

\n

Basic Usage

\n

Use useChildren in parent to associate child components:

\n
import { ref } from 'vue';\nimport { useChildren } from '@vant/use';\n\nconst RELATION_KEY = Symbol('my-relation');\n\nexport default {\n  setup() {\n    const { linkChildren } = useChildren(RELATION_KEY);\n\n    const count = ref(0);\n    const add = () => {\n      count.value++;\n    };\n\n    // provide data and methods to children\n    linkChildren({ add, count });\n  },\n};\n
\n

Use useParent in child component to get the data and methods provided by parent.

\n
import { useParent } from '@vant/use';\n\nexport default {\n  setup() {\n    const { parent } = useParent(RELATION_KEY);\n\n    // use data and methods provided by parent\n    if (parent) {\n      parent.add();\n      console.log(parent.count.value); // -> 1\n    }\n  },\n};\n
\n

API

\n

Type Declarations

\n
function useParent<T>(key: string | symbol): {\n  parent?: T;\n  index?: Ref<number>;\n};\n\nfunction useChildren(key: string | symbol): {\n  children: ComponentPublicInstance[];\n  linkChildren: (value: any) => void;\n};\n
\n

Return Value of useParent

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
NameDescriptionType
parentData and methods provided by parentany
indexIndex position of the current component in all child of the parent componentRef<number>
\n

Return Value of useChildren

\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
NameDescriptionType
childrenComponent list of childrenComponentPublicInstance[]
linkChildrenFunction to provide values to child(value: any) => void
\n
'},null,8,e))}}}]);