溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Vue3父子組件互調(diào)怎么實現(xiàn)

發(fā)布時間:2022-04-14 13:44:46 來源:億速云 閱讀:347 作者:iii 欄目:開發(fā)技術(shù)

今天小編給大家分享一下Vue3父子組件互調(diào)怎么實現(xiàn)的相關(guān)知識點,內(nèi)容詳細,邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。

一、父組件調(diào)用子組件方法

下面演示為使用 setup 語法糖的情況,值得注意的是子組件需要使用 defineExpose 對外暴露方法,父組件才可以調(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">
// 一、導入
import { ref } from 'vue';
import HelloWorld from './components/HelloWorld.vue';

// 二、數(shù)據(jù)
// 第二步:定義與 ref 同名變量
const childRef = ref<any>();

// 三、函數(shù)
const getChild = () => {
    // 第三步: 調(diào)用子組件的方法或者變量,通過value
    childRef.value.doSth("隨便傳值!");
}
</script>

<style>
</style>

3、測試結(jié)果

Vue3父子組件互調(diào)怎么實現(xiàn)

4、關(guān)于 defineExpose 的官方文檔


defineExpose

使用 <script setup> 的組件是默認關(guān)閉的,也即通過模板 ref 或者 $parent 鏈獲取到的組件的公開實例,不會暴露任何在 <script setup> 中聲明的綁定。

為了在 <script setup> 組件中明確要暴露出去的屬性,使用 defineExpose 編譯器宏:

<script setup>
import { ref } from 'vue'

const a = 1
const b = ref(2)

defineExpose({
  a,
  b
})
</script>

當父組件通過模板 ref 的方式獲取到當前組件的實例,獲取到的實例會像這樣 { a: number, b: number } (ref 會和在普通實例中一樣被自動解包)。

二、子組件調(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">
// 一、導入
import HelloWorld from './components/HelloWorld.vue';

// 二、函數(shù)
// 第二步: 自定義方法
const sayHello = () => {
  console.log("hello world!");
}
</script>

<style>
</style>

3、測試結(jié)果

Vue3父子組件互調(diào)怎么實現(xiàn)

4、關(guān)于 defineEmits 的官方文檔


definePropsdefineEmits

<script setup> 中必須使用 definePropsdefineEmits API 來聲明 propsemits ,它們具備完整的類型推斷并且在 <script setup> 中是直接可用的:

<script setup>
const props = defineProps({
  foo: String
})

const emit = defineEmits(['change', 'delete'])
// setup code
</script>
  • defineProps 和 defineEmits 都是只在 <script setup> 中才能使用的編譯器宏。他們不需要導入且會隨著 <script setup> 處理過程一同被編譯掉。

  • defineProps 接收與 props 選項相同的值,defineEmits 也接收 emits 選項相同的值。

  • defineProps 和 defineEmits 在選項傳入后,會提供恰當?shù)念愋屯茢唷?/p>

  • 傳入到 defineProps 和 defineEmits 的選項會從 setup 中提升到模塊的范圍。因此,傳入的選項不能引用在 setup 范圍中聲明的局部變量。這樣做會引起編譯錯誤。但是,它可以引用導入的綁定,因為它們也在模塊范圍內(nèi)。

如果使用了 Typescript,使用純類型聲明來聲明 prop 和 emits 也是可以的。

以上就是“Vue3父子組件互調(diào)怎么實現(xiàn)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學習更多的知識,請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI