Vue3父子組件互調(diào)方法的實(shí)現(xiàn)
下面演示均為使用 setup 語(yǔ)法糖的情況!
一、父組件調(diào)用子組件方法
下面演示為使用 setup 語(yǔ)法糖的情況,值得注意的是子組件需要使用 defineExpose 對(duì)外暴露方法,父組件才可以調(diào)用!
1、子組件
<template>
</template>
<script setup lang="ts">
// 第一步:定義子組件里面的方法
const doSth = (str: string) => {
console.log("子組件的 doSth 方法執(zhí)行了!" + str);
}
// 第二步:暴露方法
defineExpose({ doSth })
</script>
<style scoped>
</style>
2、父組件
<template>
<button @click="getChild">觸發(fā)子組件方法</button>
<!-- 第一步:定義 ref -->
<HelloWorld ref="childRef" />
</template>
<script setup lang="ts">
// 一、導(dǎo)入
import { ref } from 'vue';
import HelloWorld from './components/HelloWorld.vue';
// 二、數(shù)據(jù)
// 第二步:定義與 ref 同名變量
const childRef = ref<any>();
// 三、函數(shù)
const getChild = () => {
// 第三步: 調(diào)用子組件的方法或者變量,通過(guò)value
childRef.value.doSth("隨便傳值!");
}
</script>
<style>
</style>
3、測(cè)試結(jié)果

4、關(guān)于 defineExpose 的官方文檔
網(wǎng)址:https://v3.cn.vuejs.org/api/sfc-script-setup.html#defineexpose
defineExpose
使用 <script setup> 的組件是默認(rèn)關(guān)閉的,也即通過(guò)模板 ref 或者 $parent 鏈獲取到的組件的公開(kāi)實(shí)例,不會(huì)暴露任何在 <script setup> 中聲明的綁定。
為了在 <script setup> 組件中明確要暴露出去的屬性,使用 defineExpose 編譯器宏:
<script setup>
import { ref } from 'vue'
const a = 1
const b = ref(2)
defineExpose({
a,
b
})
</script>
當(dāng)父組件通過(guò)模板 ref 的方式獲取到當(dāng)前組件的實(shí)例,獲取到的實(shí)例會(huì)像這樣 { a: number, b: number } (ref 會(huì)和在普通實(shí)例中一樣被自動(dòng)解包)。
二、子組件調(diào)用父組件方法
1、子組件
<template>
</template>
<script setup lang="ts">
import { onMounted } from "@vue/runtime-core";
const emit = defineEmits([ "doSth" ]);
const doSth = () => {
emit('doSth');
}
onMounted(() => {
doSth();
});
</script>
<style scoped>
</style>
2、父組件
<template>
<!-- 第一步:使用 @do-sth 或 @doSth 接受方法 -->
<HelloWorld @doSth="sayHello" />
</template>
<script setup lang="ts">
// 一、導(dǎo)入
import HelloWorld from './components/HelloWorld.vue';
// 二、函數(shù)
// 第二步: 自定義方法
const sayHello = () => {
console.log("hello world!");
}
</script>
<style>
</style>
3、測(cè)試結(jié)果

4、關(guān)于 defineEmits 的官方文檔
網(wǎng)址:https://v3.cn.vuejs.org/api/sfc-script-setup.html#defineprops-和-defineemits
defineProps 和 defineEmits
在 <script setup> 中必須使用 defineProps 和 defineEmits API 來(lái)聲明 props 和 emits ,它們具備完整的類(lèi)型推斷并且在 <script setup> 中是直接可用的:
<script setup>
const props = defineProps({
foo: String
})
const emit = defineEmits(['change', 'delete'])
// setup code
</script>
- defineProps 和 defineEmits 都是只在 <script setup> 中才能使用的編譯器宏。他們不需要導(dǎo)入且會(huì)隨著 <script setup> 處理過(guò)程一同被編譯掉。
- defineProps 接收與 props 選項(xiàng)相同的值,defineEmits 也接收 emits 選項(xiàng)相同的值。
- defineProps 和 defineEmits 在選項(xiàng)傳入后,會(huì)提供恰當(dāng)?shù)念?lèi)型推斷。
- 傳入到 defineProps 和 defineEmits 的選項(xiàng)會(huì)從 setup 中提升到模塊的范圍。因此,傳入的選項(xiàng)不能引用在 setup 范圍中聲明的局部變量。這樣做會(huì)引起編譯錯(cuò)誤。但是,它可以引用導(dǎo)入的綁定,因?yàn)樗鼈円苍谀K范圍內(nèi)。
如果使用了 Typescript,使用純類(lèi)型聲明來(lái)聲明 prop 和 emits 也是可以的。
到此這篇關(guān)于Vue3父子組件互調(diào)方法的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vue3父子組件互調(diào)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue.js實(shí)現(xiàn)單選框、復(fù)選框和下拉框示例
本篇文章主要介紹了vue.js實(shí)現(xiàn)單選框、復(fù)選框和下拉框示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
VueJs中toRef與toRefs函數(shù)對(duì)比詳解
這篇文章主要為大家介紹了VueJs中toRef與toRefs函數(shù)對(duì)比詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
vue3中defineProps傳值使用ref響應(yīng)式失效詳解
這篇文章主要給大家介紹了關(guān)于vue3中defineProps傳值使用ref響應(yīng)式失效的相關(guān)資料,文章通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03
詳解如何使用vue-cli腳手架搭建Vue.js項(xiàng)目
這篇文章主要介紹了詳解如何使用vue-cli腳手架搭建Vue.js項(xiàng)目 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05
VUE : vue-cli中去掉路由中的井號(hào)#操作
這篇文章主要介紹了VUE : vue-cli中去掉路由中的井號(hào)#操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09

