五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

React、Vue3中父組件如何調(diào)用子組件內(nèi)部的方法

2023-07-15 14:57 作者:chaojilaji123  | 我要投稿

原文合集地址如下,有需要的朋友可以關(guān)注

本文地址

合集地址

React

當(dāng)父組件需要調(diào)用子組件的方法時,可以通過useImperativeHandle鉤子函數(shù)實現(xiàn)。以下例子是ts實現(xiàn)方式。

  • 在子組件中使用 useImperativeHandle 鉤子,將指定的方法暴露給父組件,以便父組件可以通過子組件的引用來調(diào)用該方法。 在子組件中使用了 useImperativeHandle 鉤子將 someMethod 方法暴露給父組件。注意,為了使用 useImperativeHandle,需要將子組件包裹在 forwardRef 函數(shù)中,并在參數(shù)列表中添加 ref。

//?子組件
import?React,?{?forwardRef,?useImperativeHandle,?useRef?}?from?'react';

type?ChildProps?=?{
??//?子組件的其他?props
};

type?ChildMethods?=?{
??//?子組件暴露給父組件的方法
??someMethod:?()?=>?void;
};

const?ChildComponent:?React.ForwardRefRenderFunction<ChildMethods,?ChildProps>?=?({},?ref)?=>?{
??//?子組件的其他代碼...

??const?someMethod?=?()?=>?{
????//?子組件的方法實現(xiàn)
????console.log('Child?method?called!');
??};

??//?將子組件的方法暴露給父組件
??useImperativeHandle(ref,?()?=>?({
????someMethod,
??}));

??return?<div>Child?Component</div>;
};

export?default?forwardRef(ChildComponent);

上述代碼中 React.ForwardRefRenderFunction 是 TypeScript 中的一個泛型類型,用于定義 forwardRef 的 render 函數(shù)的類型。 在這個類型參數(shù)中,ChildMethods 表示子組件暴露給父組件的方法的類型,ChildProps 表示子組件的 props 類型。({}) 是 render 函數(shù)的參數(shù)列表,表示子組件接收的 props,此處為空對象,即沒有額外的 props。refforwardRef 傳遞的 ref 參數(shù),用于獲取對子組件實例的引用。 總而言之,React.ForwardRefRenderFunction<ChildMethods, ChildProps> 定義了一個 forwardRef 的 render 函數(shù)類型,接收的 props 類型為 ChildProps,暴露給父組件的方法的類型為 ChildMethods,而在具體的函數(shù)實現(xiàn)中,參數(shù)列表為空對象,并接收 ref 參數(shù)用于獲取對子組件實例的引用。 這些是常見的父組件調(diào)用子組件內(nèi)部方法的方式。

有了上面的子組件,在父組件中,可以使用 useRef 鉤子來創(chuàng)建一個對子組件的引用,并通過引用調(diào)用子組件的方法:

//?父組件
import?React,?{?useRef?}?from?'react';
import?ChildComponent,?{?ChildMethods?}?from?'./ChildComponent';

const?ParentComponent:?React.FC?=?()?=>?{
??const?childRef?=?useRef<ChildMethods>(null);

??const?handleClick?=?()?=>?{
????//?通過子組件的引用調(diào)用子組件的方法
????if?(childRef.current)?{
??????childRef.current.someMethod();
????}
??};

??return?(
????<div>
??????<ChildComponent?ref={childRef}?/>
??????<button?onClick={handleClick}>Call?Child?Method</button>
????</div>

??);
};

export?default?ParentComponent;

Vue3

在 Vue 3 中,父組件調(diào)用子組件內(nèi)部的方法可以通過下面的方式實現(xiàn):

使用 $refs 引用子組件:

  • 在父組件中使用 ref 給子組件添加一個引用,并通過該引用調(diào)用子組件的方法。

  • 注意:在 Vue 3 中,$refs 不再自動包含子組件實例,而是返回一個組件實例或 DOM 元素的直接引用。

<!--?子組件?-->
<template>
??<div>
????<button?@click="childMethod">Click?Me</button>
??</div>
</template>


<script>
export?default?{
??methods:?{
????childMethod()?{
??????console.log('Child?method?called!');
????}
??}
};
</script>


<!--?父組件?-->
<template>
??<div>
????<ChildComponent?ref="childRef"?/>
????<button?@click="callChildMethod">Call?Child?Method</button>
??</div>
</template>


<script>
import?{?ref?}?from?'vue';
import?ChildComponent?from?'./ChildComponent.vue';

export?default?{
??components:?{
????ChildComponent
??},
??setup()?{
????const?childRef?=?ref(null);

????const?callChildMethod?=?()?=>?{
??????childRef.value.childMethod();
????};

????return?{
??????childRef,
??????callChildMethod
????};
??}
};
</script>


React、Vue3中父組件如何調(diào)用子組件內(nèi)部的方法的評論 (共 條)

分享到微博請遵守國家法律
龙南县| 临清市| 清河县| 盘山县| 江达县| 龙游县| 蓝田县| 金溪县| 舟山市| 通榆县| 攀枝花市| 佛山市| 麻江县| 张家界市| 土默特右旗| 离岛区| 什邡市| 海宁市| 平遥县| 荣昌县| 敦化市| 耿马| 怀化市| 镇坪县| 修武县| 忻城县| 许昌市| 灵宝市| 承德市| 麻城市| 玉龙| 江孜县| 丰城市| 牙克石市| 乐业县| 谢通门县| 靖州| 若羌县| 吉林省| 无锡市| 龙南县|