自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Vue 3 中的七個(gè)組件通信技巧

開發(fā) 前端
本文采用<script setup />的寫法,比options API更自由。那么我們就來說說以下七種組件通信方式都有哪些吧。

寫在前面

本文采用<script setup />的寫法,比options API更自由。那么我們就來說說以下七種組件通信方式:

  1. props
  2. emit
  3. v-model
  4. refs
  5. provide/inject
  6. eventBus
  7. vuex/pinia

舉個(gè)例子

本文將使用下面的演示,如下圖所示:

上圖中,列表和輸入框分別是父組件和子組件。根據(jù)不同的通信方式,父子組件會(huì)有所調(diào)整。

1. Props

Props是Vue中最常見的父子通信方式,使用起來也比較簡單。

根據(jù)上面的demo,我們在父組件中定義數(shù)據(jù)和對(duì)數(shù)據(jù)的操作,子組件只渲染一個(gè)列表。

父組件代碼如下:

<template>
<!-- child component -->
<child-components :list="list"></child-components>
<!-- parent component -->
<div class="child-wrap input-group">
<input
v-model="value"
type="text"
class="form-control"
placeholder="Please enter"
/>
<div class="input-group-append">
<button @click="handleAdd" class="btn btn-primary" type="button">
add
</button>
</div>
</div>
</template>
<script setup>
import { ref } from 'vue'
import ChildComponents from './child.vue'
const list = ref(['JavaScript', 'HTML', 'CSS'])
const value = ref('')
// event handling function triggered by add
const handleAdd = () => {
list.value.push(value.value)
value.value = ''
}
</script>

子組件只需要渲染父組件傳過來的值即可。

代碼如下:

<template>
<ul class="parent list-group">
<li class="list-group-item" v-for="i in props.list" :key="i">{{ i }}</li>
</ul>
</template>
<script setup>
import { defineProps } from 'vue'
const props = defineProps({
list: {
type: Array,
default: () => [],
},
})
</script>

2. emit

Emit也是Vue中最常見的組件通信方式,用于子組件向父組件傳遞消息。

我們在父組件中定義列表,子組件只需要傳遞添加的值即可。

子組件代碼如下:

<template>
<div class="child-wrap input-group">
<input
v-model="value"
type="text"
class="form-control"
placeholder="Please enter"
/>
<div class="input-group-append">
<button @click="handleSubmit" class="btn btn-primary" type="button">
add
</button>
</div>
</div>
</template>
<script setup>
import { ref, defineEmits } from 'vue'
const value = ref('')
const emits = defineEmits(['add'])
const handleSubmit = () => {
emits('add', value.value)
value.value = ''
}
</script>

單擊子組件中的 [Add] 按鈕后,我們發(fā)出自定義事件并將添加的值作為參數(shù)傳遞給父組件。

父組件代碼如下:

<template>
<!-- parent component -->
<ul class="parent list-group">
<li class="list-group-item" v-for="i in list" :key="i">{{ i }}</li>
</ul>
<!-- child component -->
<child-components @add="handleAdd"></child-components>
</template>
<script setup>
import { ref } from 'vue'
import ChildComponents from './child.vue'
const list = ref(['JavaScript', 'HTML', 'CSS'])
// event handling function triggered by add
const handleAdd = value => {
list.value.push(value)
}
</script>

在父組件中,只需要監(jiān)聽子組件的自定義事件,然后執(zhí)行相應(yīng)的添加邏輯即可。

3.v-model

v-model是Vue中一個(gè)優(yōu)秀的語法糖,比如下面的代碼。

<ChildComponent v-model:title="pageTitle" />

這是以下代碼的簡寫形式

<ChildComponent :title="pageTitle" @update:title="pageTitle = $event" />

確實(shí)容易多了?,F(xiàn)在我們將使用 v-model 來實(shí)現(xiàn)上面的例子。

子組件

<template>
<div class="child-wrap input-group">
<input
v-model="value"
type="text"
class="form-control"
placeholder="Please enter"
/>
<div class="input-group-append">
<button @click="handleAdd" class="btn btn-primary" type="button">
add
</button>
</div>
</div>
</template>
<script setup>
import { ref, defineEmits, defineProps } from 'vue'
const value = ref('')
const props = defineProps({
list: {
type: Array,
default: () => [],
},
})
const emits = defineEmits(['update:list'])
// Add action
const handleAdd = () => {
const arr = props.list
arr.push(value.value)
emits('update:list', arr)
value.value = ''
}
</script>

在子組件中,我們先定義props和emit,添加完成后,再emit指定的事件。

注意:update:*是Vue中固定的寫法,*代表props中的一個(gè)屬性名。

在父組件中使用比較簡單,代碼如下:

<template>
<!-- parent component -->
<ul class="parent list-group">
<li class="list-group-item" v-for="i in list" :key="i">{{ i }}</li>
</ul>
<!-- child component -->
<child-components v-model:list="list"></child-components>
</template>
<script setup>
import { ref } from 'vue'
import ChildComponents from './child.vue'
const list = ref(['JavaScript', 'HTML', 'CSS'])
</script>

4. refs

在使用option API時(shí),我們可以通過this.$refs.name獲取指定的元素或組件,而在combined API中則不行。如果我們想通過ref獲取,需要定義一個(gè)同名的Ref對(duì)象,組件掛載后才能訪問到。

示例代碼如下:

<template>
<ul class="parent list-group">
<li class="list-group-item" v-for="i in childRefs?.list" :key="i">
{{ i }}
</li>
</ul>
<!-- The value of the child component ref is the same as that in the <script> -->
<child-components ref="childRefs"></child-components>
<!-- parent component -->
</template>
<script setup>
import { ref } from 'vue'
import ChildComponents from './child.vue'
const childRefs = ref(null)
</script>

子組件代碼如下:

<template>
<div class="child-wrap input-group">
<input
v-model="value"
type="text"
class="form-control"
placeholder="Please enter"
/>
<div class="input-group-append">
<button @click="handleAdd" class="btn btn-primary" type="button">
add
</button>
</div>
</div>
</template>
<script setup>
import { ref, defineExpose } from 'vue'
const list = ref(['JavaScript', 'HTML', 'CSS'])
const value = ref('')
// event handling function triggered by add
const handleAdd = () => {
list.value.push(value.value)
value.value = ''
}
defineExpose({ list })
</script>

注意:默認(rèn)情況下,setup組件是關(guān)閉的,通過template ref獲取組件的public實(shí)例。如果需要暴露,需要通過defineExpose API暴露。

5. provide/inject

Provide 和 inject 是 Vue 中提供的一對(duì) API。無論層次有多深,API都能實(shí)現(xiàn)父組件到子組件的數(shù)據(jù)傳遞。

示例代碼如下所示:

父組件

<template>
<!-- child component -->
<child-components></child-components>
<!-- parent component -->
<div class="child-wrap input-group">
<input
v-model="value"
type="text"
class="form-control"
placeholder="Please enter"
/>
<div class="input-group-append">
<button @click="handleAdd" class="btn btn-primary" type="button">
add
</button>
</div>
</div>
</template>
<script setup>
import { ref, provide } from 'vue'
import ChildComponents from './child.vue'
const list = ref(['JavaScript', 'HTML', 'CSS'])
const value = ref('')
// Provide data to child components.
provide('list', list.value)
// event handling function triggered by add
const handleAdd = () => {
list.value.push(value.value)
value.value = ''
}
</script>

子組件

<template>
<ul class="parent list-group">
<li class="list-group-item" v-for="i in list" :key="i">{{ i }}</li>
</ul>
</template>
<script setup>
import { inject } from 'vue'
// Accept data provided by parent component
const list = inject('list')
</script>

注意:使用provide進(jìn)行數(shù)據(jù)傳遞時(shí),盡量用readonly封裝數(shù)據(jù),避免子組件修改父組件傳過來的數(shù)據(jù)。

6.eventBus

在 Vue 3 中移除了 eventBus,但可以借助第三方工具來完成。Vue 官方推薦 mitt 或 tiny-emitter。大多數(shù)情況下,不推薦使用全局事件總線來實(shí)現(xiàn)組件通信。雖然比較簡單粗暴,但是長期維護(hù)event bus是個(gè)大問題,這里就不多說了。具體可以閱讀具體工具的文檔。

7.Vuex && Pinia

Vuex和Pinia是Vue 3中的狀態(tài)管理工具,使用這兩個(gè)工具可以輕松實(shí)現(xiàn)組件通信。由于這兩個(gè)工具比較強(qiáng)大,這里就不展示了。有關(guān)詳細(xì)信息,請參閱文檔。

最后

以上就是我今天想與你分享的Vue3中的7個(gè)組件通信技巧,如果對(duì)你有幫助的話,請記得點(diǎn)贊我,關(guān)注我,并將這篇文章分享給你的朋友,也許能夠幫助到他。

最后,謝謝你的閱讀。

責(zé)任編輯:華軒 來源: web前端開發(fā)
相關(guān)推薦

2022-05-06 08:47:10

Vue 3組件前端

2023-12-19 16:50:37

2022-12-12 13:19:11

Vue3開發(fā)技巧

2022-11-30 15:33:39

Vue 3組件

2023-03-29 07:54:25

Vue 3插件

2023-09-07 16:28:46

JavaScrip

2021-11-22 12:13:54

Linuxwget 命令

2022-06-23 09:22:57

Vue技巧前端

2023-11-06 11:32:46

CSS選擇器作用域

2023-05-30 09:59:38

2018-05-24 08:47:15

數(shù)據(jù)存儲(chǔ)技巧

2021-08-17 10:08:44

HTML網(wǎng)站網(wǎng)絡(luò)

2024-06-25 15:41:41

2022-04-14 10:40:11

領(lǐng)導(dǎo)者IT團(tuán)隊(duì)遠(yuǎn)程團(tuán)隊(duì)

2022-03-11 12:31:04

Vue3組件前端

2019-09-09 10:32:51

基于意圖的網(wǎng)絡(luò)IBN網(wǎng)絡(luò)

2012-09-17 10:57:39

郵件安全

2021-03-02 10:54:08

高管IT投資首席信息官

2022-08-26 08:00:00

數(shù)字時(shí)代IT首席信息官

2015-11-30 17:12:31

Git使用技巧
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)