成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Vue 3 中的七個組件通信技巧

開發(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

舉個例子

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

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

1. Props

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

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

父組件代碼如下:

<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中一個優(yōu)秀的語法糖,比如下面的代碼。

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

這是以下代碼的簡寫形式

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

確實容易多了。現(xiàn)在我們將使用 v-model 來實現(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中的一個屬性名。

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

<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時,我們可以通過this.$refs.name獲取指定的元素或組件,而在combined API中則不行。如果我們想通過ref獲取,需要定義一個同名的Ref對象,組件掛載后才能訪問到。

示例代碼如下:

<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>

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

5. provide/inject

Provide 和 inject 是 Vue 中提供的一對 API。無論層次有多深,API都能實現(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進行數(shù)據(jù)傳遞時,盡量用readonly封裝數(shù)據(jù),避免子組件修改父組件傳過來的數(shù)據(jù)。

6.eventBus

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

7.Vuex && Pinia

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

最后

以上就是我今天想與你分享的Vue3中的7個組件通信技巧,如果對你有幫助的話,請記得點贊我,關(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 命令

2023-05-30 09:59:38

2018-05-24 08:47:15

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

2023-11-06 11:32:46

CSS選擇器作用域

2022-06-23 09:22:57

Vue技巧前端

2021-08-17 10:08:44

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

2022-04-14 10:40:11

領(lǐng)導(dǎo)者IT團隊遠程團隊

2024-06-25 15:41:41

2023-12-15 08:51:48

2015-11-30 17:12:31

Git使用技巧

2021-03-02 10:54:08

高管IT投資首席信息官

2012-09-17 10:57:39

郵件安全

2022-08-26 08:00:00

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

2021-06-10 08:00:00

首席信息安全官IT數(shù)據(jù)
點贊
收藏

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

主站蜘蛛池模板: 91精品国产乱码久久久久久久久 | 91 在线| 91久久久久 | 91电影| 国产精品中文 | 中文字幕在线看第二 | 日韩一级免费电影 | 国产午夜精品理论片a大结局 | 精品91视频 | 九九热九九| 久久99精品国产麻豆婷婷 | 在线看91 | 97久久精品午夜一区二区 | 黄色片在线免费看 | 精品中文字幕一区二区 | 欧美精品一二区 | 视频一区二区在线观看 | 国产视频线观看永久免费 | 色婷婷九月 | 久久毛片 | 欧美日韩a| 视频二区国产 | 99pao成人国产永久免费视频 | 欧美日韩高清一区 | 欧美成人免费在线视频 | 国产精品精品视频一区二区三区 | 亚洲男人天堂网 | 日韩一区二区三区在线观看 | 精品不卡| 亚洲精品日韩一区二区电影 | 久久一级大片 | 国产精品久久久久久久久久久久冷 | 欧美精品一区二区三 | 精品久久电影 | 国产欧美视频一区二区 | 91久久久www播放日本观看 | 国产精品久久久99 | 亚洲精品久久久久久首妖 | 久久久综合久久 | 三级视频在线观看电影 | 国产免费一区二区三区免费视频 |