vue3中的父子組件通訊詳情
一、傳統(tǒng)的props
通過在父組件中給子組件傳值,然后在子組件中通過props接受數(shù)據(jù)
//父組件
<ValidateInput
type="text"
v-model="emailVal"
:rules='emailRules'
placeholder='請(qǐng)輸入郵箱地址'
ref="inputRef"
>
</ValidateInput>
//子組件
export default defineComponent({
name: 'ValidateInput',
props: {
rules: Array as PropType <RulesProp>,
modelValue: String
},
}二、通過modeValue綁定
//v-model簡寫
<ValidateInput
type="text"
v-model="emailVal"
placeholder='請(qǐng)輸入郵箱地址'
ref="inputRef"
>
</ValidateInput>
//實(shí)際上是
<ValidateInput
type="text"
:emailVal="emailVal"
@update:modelValue="方法"
placeholder='請(qǐng)輸入郵箱地址'
ref="inputRef"
>
</ValidateInput>
//子組件
<template>
<div class="inputItem">
<input
type="text"
:value="inputRef.val"
@input="updateValue"
id="emial"
>
</div>
</template>
export default defineComponent({
name: 'ValidateInput',
props: {
rules: Array as PropType <RulesProp>,
modelValue: String
},
setup (props, context) {
const inputRef = reactive({
val: props.modelValue || '',
isError: false,
message: ''
})
const updateValue = (e:KeyboardEvent) => {
const targetValue = (e.target as HTMLInputElement).value
inputRef.val = targetValue
context.emit('update:modelValue', targetValue)
}
return {
inputRef,
updateValue
}
}三、事件廣播(vue3中$on和$emit已廢棄),使用新的插件mitt
Vue3.0版本中把on,off、onece等事件函數(shù)移除了,emit()用于父子組件之間的溝通。為了能夠使用事務(wù)總線,除了emit觸發(fā)函數(shù)還得有on監(jiān)聽函數(shù)。官方推薦使用第三方庫mitt
首先安裝第三方庫mitt
npm install --save mitt
然后在程序中使用事件總線:
//父組件接受'form-item-created'頻道
<script lang="ts">
import { defineComponent, onUnmounted } from 'vue'
import mitt from 'mitt'
export const emitter = mitt()
export default defineComponent({
name: 'ValidateForm',
setup () {
const callback = (test: string) => {
console.log(test)
}
emitter.on('form-item-created', callback)
onUnmounted(() => {
emitter.off('form-item-created', callback)
})
return {}
}
})
</script>
//子組件發(fā)送信息
onMounted(() => {
console.log(inputRef.val)
emitter.emit('form-item-created', inputRef.val)
})到此這篇關(guān)于vu3中的父子組件通訊詳情的文章就介紹到這了,更多相關(guān)vu3組件通訊內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+element搭建后臺(tái)小總結(jié) el-dropdown下拉功能
這篇文章主要為大家詳細(xì)介紹了vue+element搭建后臺(tái)小總結(jié),el-dropdown下拉功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
vue項(xiàng)目如何部署運(yùn)行到tomcat上
這篇文章主要介紹了vue項(xiàng)目如何部署運(yùn)行到tomcat上的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
詳解從react轉(zhuǎn)職到vue開發(fā)的項(xiàng)目準(zhǔn)備
這篇文章主要介紹了詳解從react轉(zhuǎn)職到vue開發(fā)的項(xiàng)目準(zhǔn)備,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01
vue-cli3 設(shè)置端口號(hào)(81)無效的解決
這篇文章主要介紹了vue-cli3 設(shè)置端口號(hào)(81)無效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Vue?Router嵌套路由(children)的用法小結(jié)
嵌套路由就是父路由里面嵌套他的子路由,父路由有自己的路由導(dǎo)航和路由容器(router-link、router-view),通過配置children可實(shí)現(xiàn)多層嵌套,這篇文章主要介紹了Vue--Router--嵌套路由(children)的用法,需要的朋友可以參考下2022-08-08
Vue中this.$router.push參數(shù)獲取方法
下面小編就為大家分享一篇Vue中this.$router.push參數(shù)獲取方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02
Vue實(shí)現(xiàn)購物車場(chǎng)景下的應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)購物車場(chǎng)景下的應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11

