国产chinesehdxxxx野外,国产av无码专区亚洲av琪琪,播放男人添女人下边视频,成人国产精品一区二区免费看,chinese丰满人妻videos

Vant3 useRelation

2021-09-07 17:26 更新

介紹

建立父子組件之間的關聯(lián)關系,進行數(shù)據(jù)通信和方法調用,基于 provide 和 inject 實現(xiàn)。

代碼演示

基本用法

在父組件中使用 useChildren 關聯(lián)子組件:

import { ref } from 'vue';
import { useChildren } from '@vant/use';

const RELATION_KEY = Symbol('my-relation');

export default {
  setup() {
    const { linkChildren } = useChildren(RELATION_KEY);

    const count = ref(0);
    const add = () => {
      count.value++;
    };

    // 向子組件提供數(shù)據(jù)和方法
    linkChildren({ add, count });
  },
};

在子組件中使用 useParent 獲取父組件提供的數(shù)據(jù)和方法:

import { useParent } from '@vant/use';

export default {
  setup() {
    const { parent } = useParent(RELATION_KEY);

    // 調用父組件提供的數(shù)據(jù)和方法
    if (parent) {
      parent.add();
      console.log(parent.count.value); // -> 1
    }
  },
};

API

類型定義

function useParent<T>(
  key: string | symbol
): {
  parent?: T;
  index?: Ref<number>;
};

function useChildren(
  key: string | symbol
): {
  children: ComponentPublicInstance[];
  linkChildren: (value: any) => void;
};

useParent 返回值

參數(shù) 說明 類型
parent 父組件提供的值 any
index 當前組件在父組件的所有子組件中對應的索引位置 Ref<number>

useChildren 返回值

參數(shù) 說明 類型
children 子組件列表 ComponentPublicInstance[]
linkChildren 向子組件提供值的方法 (value: any) => void


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號