詳解Vue3?SFC?和?TSX?方式調(diào)用子組件中的函數(shù)
在開(kāi)發(fā)中會(huì)遇到這樣的需求:獲取子組件的引用,并調(diào)用子組件中定義的方法。如封裝了一個(gè)表單組件,在父組件中需要調(diào)用這個(gè)表單組件的引用,并調(diào)用這個(gè)表單組件的校驗(yàn)表單函數(shù)或重置表單函數(shù)。要實(shí)現(xiàn)這個(gè)功能,首先要在子組件中暴露父組件需要調(diào)用的函數(shù),然后去父組件中獲取子組件的引用,最后通過(guò)子組件的引用調(diào)用子組件暴露的方法。
1 子組件暴露方法
1.1 SFC(.vue)暴露方法
在使用 .vue 定義的組件中,setup 中提供了 defineExpose() 方法,該方法可以將組件內(nèi)部的方法暴露給父組件。
創(chuàng)建子組件 demo-component-sfc.vue:
<template>
<el-button type="primary" @click="demoFun('child')">demo component sfc</el-button>
</template>
<script lang="ts" setup name="demo-component-sfc">
const demoFun = (str: string) => {
console.log('demo component sfc', str)
}
// 使用 defineExpose 暴露組件內(nèi)部的方法
defineExpose({ demoFun })
</script>
1.2 TSX(.tsx)暴露方法
使用 .tsx 方式定義的組件,也是通過(guò)參數(shù) context 中的 expose() 方法暴露組件內(nèi)容的方法。
創(chuàng)建子組件 demo-component-tsx.tsx:
import { defineComponent } from 'vue'
export default defineComponent({
name: 'demo-component-tsx',
setup (props, context) {
const demoFun = (str: string) => {
console.log('demo component tsx', str)
}
// 使用 expose 暴露組件內(nèi)部的方法
context.expose({ demoFun })
return () => (
<el-button type="primary" onClick={() => demoFun('child')}>demo component tsx</el-button>
)
}
})
2 父組件調(diào)用子組件中的方法
2.1 SFC(.vue)調(diào)用
在 .vue 文件中獲取組件引用首先定義一個(gè) ref 變量,然后為子組件設(shè)置 ref 屬性。ref 屬性值與變量名要保持一致。
import { defineComponent } from 'vue'
export default defineComponent({
name: 'demo-component-tsx',
setup (props, context) {
const demoFun = (str: string) => {
console.log('demo component tsx', str)
}
// 使用 expose 暴露組件內(nèi)部的方法
context.expose({ demoFun })
return () => (
<el-button type="primary" onClick={() => demoFun('child')}>demo component tsx</el-button>
)
}
})
如上面的代碼所示:第一個(gè)子組件的 ref 屬性值為 sfcRef,定義的變量名也是 sfcRef。在父組件中便可以使用 sfcRef 調(diào)用子組件的 demoFun 方法了。
2.2 TSX(.tsx)調(diào)用
在 .tsx 中獲取組件的引用更簡(jiǎn)單,首先定義一個(gè) ref 變量,然后將該變量設(shè)置給子組件的 ref 屬性即可。
import { defineComponent, ref } from 'vue'
import DemoComponentSfc from '@/components/ref/demo-component-sfc.vue'
import DemoComponentTsx from '@/components/ref/demo-component-tsx'
export default defineComponent({
name: 'demo-ref-tsx',
setup () {
const sfcRef = ref()
const onBtnClick1 = () => {
if (sfcRef.value) {
sfcRef.value && sfcRef.value.demoFun('parent')
}
}
const tsxRef = ref()
const onBtnClick2 = () => {
if (tsxRef.value) {
tsxRef.value && tsxRef.value.demoFun('parent')
}
}
return () => (
<>
<div>
<DemoComponentSfc ref={sfcRef} />
<el-button onClick={onBtnClick1}>parent button</el-button>
</div>
<div style="margin-top: 10px;">
<DemoComponentTsx ref={tsxRef} />
<el-button onClick={onBtnClick2}>parent button</el-button>
</div>
</>
)
}
})
兩者實(shí)現(xiàn)效果一致:

到此這篇關(guān)于Vue3 SFC 和 TSX 方式調(diào)用子組件中的函數(shù)的文章就介紹到這了,更多相關(guān)Vue調(diào)用子組件中的函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue中靜態(tài)文件引用的注意事項(xiàng)及說(shuō)明
這篇文章主要介紹了vue中靜態(tài)文件引用的注意事項(xiàng)及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
vue3+vue-router+vite實(shí)現(xiàn)動(dòng)態(tài)路由的全過(guò)程
動(dòng)態(tài)路由是根據(jù)不同情況實(shí)時(shí)變化的路由,在權(quán)限管理系統(tǒng)中,動(dòng)態(tài)路由常用于根據(jù)用戶角色分配不同的菜單和功能,這篇文章主要介紹了vue3+vue-router+vite實(shí)現(xiàn)動(dòng)態(tài)路由的相關(guān)資料,需要的朋友可以參考下2024-10-10
Vue?運(yùn)行高德地圖官方樣例,設(shè)置class無(wú)效的解決
這篇文章主要介紹了Vue?運(yùn)行高德地圖官方樣例,設(shè)置class無(wú)效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Ant Design Vue全局對(duì)話確認(rèn)框(confirm)的回調(diào)不觸發(fā)
這篇文章主要介紹了Ant Design Vue全局對(duì)話確認(rèn)框(confirm)的回調(diào)不觸發(fā)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
基于Vue3和Plotly.js實(shí)現(xiàn)交互式3D圖
這篇文章主要介紹了基于Vue3和Plotly.js實(shí)現(xiàn)交互式3D圖,本代碼旨在為數(shù)據(jù)可視化提供一個(gè)交互式圖表,允許用戶動(dòng)態(tài)控制圖表中線條的顏色和可見(jiàn)性,此功能對(duì)于探索大型數(shù)據(jù)集或突出特定數(shù)據(jù)子集非常有用,需要的朋友可以參考下2024-07-07
vue封裝實(shí)現(xiàn)自動(dòng)循環(huán)滾動(dòng)的列表
在做數(shù)據(jù)大屏開(kāi)發(fā)的過(guò)程中,經(jīng)常出現(xiàn)需要對(duì)列表進(jìn)行自動(dòng)滾動(dòng)的需求,所以本文就來(lái)為大家介紹一下如何利用vue封裝一個(gè)自動(dòng)循環(huán)滾動(dòng)的列表吧2023-09-09
vue 數(shù)據(jù)遍歷篩選 過(guò)濾 排序的應(yīng)用操作
這篇文章主要介紹了vue 數(shù)據(jù)遍歷篩選 過(guò)濾 排序的應(yīng)用操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
Vue2.5通過(guò)json文件讀取數(shù)據(jù)的方法
本文通過(guò)實(shí)例代碼給大家詳細(xì)介紹了Vue2.5通過(guò)json文件讀取數(shù)據(jù)的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-02-02

