Vue中的父子組件傳值及傳方法
傳值
父組件傳值給子組件
// 父組件
<template>
<div>
<!-- 父組件中引入子組件,并傳入子組件內(nèi)需要的值 -->
<child :message="father"></child>
</div>
</template>
<script>
import child from './child'
export default {
data() {
return {
//定義需要傳入的值
father: '傳給子組件的值'
}
},
components: {
child
}
}
</script>
// 子組件
<template>
<div>
<div>{{message}}</div>
</div>
</template>
<script>
export default {
//在子組件中注冊(cè)props,并使用父組件中傳遞過來的數(shù)據(jù)
props: {
message: String
},
}
</script>
子組件傳值給父組件
// 父組件
<template>
<div>
<!--自定義事件child,事件名為parent(parent事件用于接收子組件傳遞過來的值)-->
<parent @child="parent"></parent >
</div>
</template>
<script>
export default {
data: {
message: ''
},
methods: {
parent(val) {
this.message = val;
}
}
}
</script>
// 子組件
<template>
<div>
<!--點(diǎn)擊按鈕觸發(fā)send事件,把message傳給父組件-->
<button @click="send">Send</button>
</div>
</template>
<script>
export default {
data() {
return {
message: '我是子組件的消息'
}
},
methods: {
sendMes() {
this.$emit('child', this.message);
}
}
}
</script>
調(diào)用方法
父組件調(diào)用子組件的方法
// 父組件
<template>
<div @click="handleParent">
<children ref="mychild"></children>
</div>
</template>
<script>
import children from 'children.vue'
export default {
components: {
children
},
methods:{
handleParent() {
this.$refs.mychild.childMethod();
}
}
}
</script>
// 子組件
<template>
<div></div>
</template>
<script>
export default {
methods:{
childMethod() {
console.log('My name is child')
}
}
}
</script>
子組件調(diào)用父組件的方法
// 父組件
<template>
<div>
<child @listenChild="showFrom"></child>
<div>{{ value }}</div>
</div>
</template>
<script>
import child from './compontents/child.vue'
export default {
components:{child},
data () {
return {
value:''
}
},
methods: {
showFrom(data){
this.value = data
},
}
}
</script>
//子組件
<template>
<button @click="send()">
我是子組件,點(diǎn)擊我向父組件傳值
</button>
</template>
<script>
export default {
data(){
return {
message:'子組件的數(shù)據(jù)'
}
},
methods:{
send() {
this.$emit("listenChild",this.message)
}
}
}
</script>
非父子組件
廣播自定義事件
handleClick(){
//response為要傳的值
this.$root.$emit('change',response)
}
處理自定義事件
handleClick(){
this.$root.$on('change',(item)=>{
console.log(item) //item就是廣播事件的response
})
}
有時(shí)候會(huì)發(fā)生事件只被 emit 觸發(fā)了一次,但是回調(diào)函數(shù)卻被執(zhí)行了多次的現(xiàn)象。這種現(xiàn)象往往發(fā)生在頁面跳轉(zhuǎn)退出后重新進(jìn)入的時(shí)候。
產(chǎn)生原因:
this.root.Bus.root.Bus. root.Bus.on 實(shí)際是向 Bus 容器中添加一個(gè)事件監(jiān)聽器,當(dāng)頁面跳轉(zhuǎn)時(shí),原來的 vue 組件被注銷,但是原來 vue 組件向 Bus 容器中添加的事件監(jiān)聽器并不會(huì)被移除。
因此,當(dāng)下次進(jìn)入這個(gè) vue 組件對(duì)應(yīng)的頁面時(shí),執(zhí)行到 this. root.Bus.root.Bus. root.Bus.on 時(shí),又會(huì)向 Bus 容器中添加一個(gè)重復(fù)的事件監(jiān)聽器,以此類推,導(dǎo)致 Bus 容器中有很多個(gè)一模一樣的事件監(jiān)聽器,從而導(dǎo)致事件只被觸發(fā)一次,但是回調(diào)函數(shù)被執(zhí)行多次的現(xiàn)象。
解決方案:
在 vue 組件的 beforeDetory 鉤子函數(shù)中將本 vue 組件往 Bus 容器中添加的時(shí)間監(jiān)聽器全部手動(dòng)移除。
//在vue對(duì)象的methods域中定義個(gè)函數(shù)專門移除事件監(jiān)聽器
offListener() {
this.$root.Bus.off("事件名");
this.$root.Bus.off("事件名");
this.$root.Bus.off("事件名");
},
//在beforeDestroy鉤子中調(diào)用此函數(shù)
beforeDestroy() {
this.offListener();
},
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Nuxt 嵌套路由nuxt-child組件用法(父子頁面組件的傳值)
這篇文章主要介紹了Nuxt 嵌套路由nuxt-child組件用法(父子頁面組件的傳值),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
element的el-upload組件上傳文件跨域問題的幾種解決
跨域問題網(wǎng)上搜索很多,感覺情況都不一樣,本文主要介紹了element的el-upload組件上傳文件跨域問題的幾種解決,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
在Vue3中使用BabylonJs開發(fā)?3D的初體驗(yàn)
這篇文章主要介紹了在?Vue3?中使用?BabylonJs?開發(fā)?3D?是什么體驗(yàn),在本文中,向您展示了如何創(chuàng)建?Vue?組件、Babylon?類、在畫布上渲染場(chǎng)景以及創(chuàng)建?3D?網(wǎng)格,需要的朋友可以參考下2022-07-07
vue導(dǎo)出word純前端的實(shí)現(xiàn)方式
這篇文章主要介紹了vue導(dǎo)出word純前端的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
element中TimePicker時(shí)間選擇器禁用部分時(shí)間(顯示禁用到分鐘)
這篇文章主要介紹了element中TimePicker時(shí)間選擇器禁用部分時(shí)間(顯示禁用到分鐘),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03

