詳解Vue3?SFC?和?TSX?方式調(diào)用子組件中的函數(shù)
在開發(fā)中會遇到這樣的需求:獲取子組件的引用,并調(diào)用子組件中定義的方法。如封裝了一個表單組件,在父組件中需要調(diào)用這個表單組件的引用,并調(diào)用這個表單組件的校驗表單函數(shù)或重置表單函數(shù)。要實現(xiàn)這個功能,首先要在子組件中暴露父組件需要調(diào)用的函數(shù),然后去父組件中獲取子組件的引用,最后通過子組件的引用調(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 方式定義的組件,也是通過參數(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 文件中獲取組件引用首先定義一個 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> ) } })
如上面的代碼所示:第一個子組件的 ref 屬性值為 sfcRef,定義的變量名也是 sfcRef。在父組件中便可以使用 sfcRef 調(diào)用子組件的 demoFun 方法了。
2.2 TSX(.tsx)調(diào)用
在 .tsx 中獲取組件的引用更簡單,首先定義一個 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> </> ) } })
兩者實現(xiàn)效果一致:
到此這篇關(guān)于Vue3 SFC 和 TSX 方式調(diào)用子組件中的函數(shù)的文章就介紹到這了,更多相關(guān)Vue調(diào)用子組件中的函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+vue-meta-info動態(tài)設(shè)置meta標簽教程
這篇文章主要介紹了vue+vue-meta-info動態(tài)設(shè)置meta標簽教程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04詳解Vue的鉤子函數(shù)(路由導(dǎo)航守衛(wèi)、keep-alive、生命周期鉤子)
這篇文章主要介紹了詳解Vue的鉤子函數(shù)(路由導(dǎo)航守衛(wèi)、keep-alive、生命周期鉤子),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07vue使用better-scroll實現(xiàn)下拉刷新、上拉加載
這篇文章主要為大家詳細介紹了vue使用better-scroll實現(xiàn)下拉刷新、上拉加載,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-11-11