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

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

開發 前端
本文采用<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

舉個例子

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

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

1. Props

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

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

父組件代碼如下:

<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] 按鈕后,我們發出自定義事件并將添加的值作為參數傳遞給父組件。

父組件代碼如下:

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

在父組件中,只需要監聽子組件的自定義事件,然后執行相應的添加邏輯即可。

3.v-model

v-model是Vue中一個優秀的語法糖,比如下面的代碼。

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

這是以下代碼的簡寫形式

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

確實容易多了?,F在我們將使用 v-model 來實現上面的例子。

子組件

<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組件是關閉的,通過template ref獲取組件的public實例。如果需要暴露,需要通過defineExpose API暴露。

5. provide/inject

Provide 和 inject 是 Vue 中提供的一對 API。無論層次有多深,API都能實現父組件到子組件的數據傳遞。

示例代碼如下所示:

父組件

<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進行數據傳遞時,盡量用readonly封裝數據,避免子組件修改父組件傳過來的數據。

6.eventBus

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

7.Vuex && Pinia

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

最后

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

最后,謝謝你的閱讀。

責任編輯:華軒 來源: web前端開發
相關推薦

2022-05-06 08:47:10

Vue 3組件前端

2023-12-19 16:50:37

2022-12-12 13:19:11

Vue3開發技巧

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選擇器作用域

2021-08-17 10:08:44

HTML網站網絡

2022-04-14 10:40:11

領導者IT團隊遠程團隊

2024-06-25 15:41:41

2023-05-30 09:59:38

2018-05-24 08:47:15

數據存儲技巧

2019-09-09 10:32:51

基于意圖的網絡IBN網絡

2022-07-14 10:34:13

IT領導者CIO首席信息官

2021-06-28 11:46:31

GitLinux

2018-04-27 09:22:21

數據存儲技巧

2025-03-21 08:20:00

數據清洗Python編程

2022-03-11 12:31:04

Vue3組件前端
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 女生羞羞网站 | 色黄爽| 色就干| 女人精96xxx免费网站p | 国产精品爱久久久久久久 | 亚洲精品一区中文字幕乱码 | 亚洲一区有码 | 日韩av免费在线观看 | 欧美午夜一区 | 精品国产乱码久久久久久蜜柚 | 欧美成视频在线观看 | 黄视频国产 | 国产精品国产成人国产三级 | 欧美视频中文字幕 | 一区二区三区四区不卡视频 | 亚洲日本激情 | 精品一区av| 男女羞羞视频在线 | 人人色视频 | 欧美日韩精品影院 | 国产精品久久久久aaaa樱花 | 久草免费在线 | 精品国产乱码久久久久久图片 | 国产美女精品 | 久久久久久99 | 一区二区在线免费播放 | 国产成人a亚洲精品 | 欧美在线激情 | 成人福利在线 | 噜噜噜噜狠狠狠7777视频 | av乱码 | 不卡一区二区三区四区 | 韩国av一区二区 | 日本不卡视频 | 综合激情久久 | 国产精品国产三级国产aⅴ浪潮 | 日日日视频 | 国产丝袜av | 中文字幕91 | 亚洲欧美日韩精品久久亚洲区 | 精品国产欧美在线 |