Vue中父子組件通信與事件觸發(fā)的深入講解
一、組件
子組件
<template> <div style="border:1px solid black;width:400px; height: 130px;"> <h3>我是子組件</h3> <button>子組件將值傳遞給父組件</button> <div>子組件接收父組件的值:</div> </div> </template>
父組件
<template> <div style="border:1px solid red;padding:2rem;width:400px;margin:0 auto;"> <h3>我是父組件</h3> <div>子組件向父組件傳遞的值:</div> <Child></Child> </div> </template> <script> import Child from './Child'; export default { components: { Child } } </script>
效果展示:
通過(guò)這張圖可以看出父子組件的結(jié)構(gòu),下面我們來(lái)實(shí)習(xí)父子組件通信。
二、父子組件通信
父組件給子組件通信
實(shí)現(xiàn)思路:子組件通過(guò) props 來(lái)接受父組件傳過(guò)來(lái)的值。
在父組件中,定義一個(gè)data變量,在子組件標(biāo)簽中動(dòng)態(tài)綁定這個(gè)值。
// Father.vue <template> <div style="border:1px solid red;padding:2rem;width:400px;margin:0 auto;"> <h3>我是父組件</h3> <div>子組件向父組件傳遞的值:{{ChildMsg}}</div> <Child :FatherMsg="data"></Child> </div> </template> <script> import Child from './Child'; export default { data() { return { data: 'I am your father', } }, components: { Child } } </script>
接著在子組件里通過(guò) props 來(lái)接收,這樣子組件就接收到了父組件傳遞過(guò)來(lái)的值了。
// Child.vue <template> <div style="border:1px solid black;width:400px; height: 130px;"> <h3>我是子組件</h3> <button>子組件將值傳遞給父組件</button> <div>父組件向子組件傳遞的值:{{FatherMsg}}</div> </div> </template> <script> export default { data() { return { data: 'I am your children', } }, props: ['FatherMsg'] } </script>
可以看到,我們父組件向子組件通信已經(jīng)實(shí)現(xiàn)了,接下來(lái)就是子組件向父組件通信了,這個(gè)就要使用到 this.$emit
方法了。
子組件向父組件通信
實(shí)現(xiàn)思路:通過(guò)在子組件中使用 this.$emit
來(lái)觸發(fā)自定義事件并傳值,然后在父組件中監(jiān)聽該事件即可。
在子組件中給 button 按鈕添加 click 事件,來(lái)通過(guò)
this.$emit
自定義事件,并傳入一個(gè)參數(shù):<template> <div style="border:1px solid black;width:400px; height: 130px;"> <h3>我是子組件</h3> <button @click="send">子組件將值傳遞給父組件</button> <div>父組件向子組件傳遞的值:{{FatherMsg}}</div> </div> </template> <script> export default { data() { return { data: 'I am your children', } }, props: ['FatherMsg'], methods: { send() { this.$emit('ListenChild', this.data); } } } </script>
在父組件中的子組件標(biāo)簽里,先在 data 里定義一個(gè)變量接收這個(gè)值,然后監(jiān)聽在子組件中自定義的事件,并接受這個(gè)參數(shù)賦值給定義的變量:
<template> <div style="border:1px solid red;padding:2rem;width:400px;margin:0 auto;"> <h3>我是父組件</h3> <div>子組件向父組件傳遞的值:{{ChildMsg}}</div> <Child :FatherMsg="data" @ListenChild="ListenChild"></Child> </div> </template> <script> import Child from './Child'; export default { data() { return { data: 'I am your father', ChildMsg: '', } }, components: { Child }, methods: { ListenChild(data) { console.log("子組件傳遞過(guò)來(lái)的值:" , data); this.ChildMsg = data; } } } </script>
點(diǎn)擊子組件中的“子組件將值傳遞給父組件”,就可看到如下效果:
三、父子組件事件觸發(fā)
父組件調(diào)用子組件中的事件方法
通過(guò) ref 直接調(diào)用子組件的方法:
// Child.vue <template> <div style="border: 1px solid black; width: 150px; margin: 10px auto"> 我是子組件 <div style="color: red"> {{ msg }} </div> </div> </template> <script> export default { data() { return { msg: '', } }, methods: { childFun() { console.log('我是子組件的方法 childFun'); this.msg = '我的方法被調(diào)用了' }, }, }; </script>
在子組件標(biāo)簽上添加 ref 屬性,然后在方法中通過(guò)
this.$refs
找到綁定 ref 的屬性調(diào)用該子組件內(nèi)的方法即可。// Father.vue <template> <div style="border: 1px solid red; width: 200px; padding: 10px; margin: 0 auto"> 我是父組件 <Button @click="handleClick">點(diǎn)擊調(diào)用子組件方法</Button> <Child ref="child" /> </div> </template> <script> import Child from './Child'; export default { components: { Child }, methods: { handleClick() { this.$refs.child.childFun(); }, }, } </script>
通過(guò)組件的
$emit
、$on
方法:// Child.vue <template> <div style="border: 1px solid black; width: 150px; margin: 10px auto"> 我是子組件 <div style="color: red"> {{ msg }} </div> </div> </template> <script> export default { data() { return { msg: '', } }, mounted() { this.$on('childFun', function() { console.log('我是子組件方法'); this.msg = '我的方法被調(diào)用了' }); } }; </script>
在子組件中使用
$on
綁定一個(gè)方法,然后在父組件中通過(guò)$emit
找到綁定$on
上面的事件名即可,但是也需要 ref 的配合。// Father.vue <template> <div style="border: 1px solid red; width: 200px; padding: 10px; margin: 0 auto"> 我是父組件 <Button @click="handleClick">點(diǎn)擊調(diào)用子組件方法</Button> <Child ref="child" /> </div> </template> <script> import Child from './Child'; export default { components: { Child }, methods: { handleClick() { //子組件$on中的名字 this.$refs.child.$emit("childFun") }, }, } </script>
兩種實(shí)現(xiàn)方式效果一致。
調(diào)用方法前:
調(diào)用方法后:
子組件調(diào)用父組件中的事件方法
直接在子組件中通過(guò)
this.$parent
來(lái)調(diào)用父組件的方法// Father.vue <template> <div style="border: 1px solid red; width: 200px; padding: 10px; margin: 0 auto" > 我是父組件 <Child></Child> <div style="color: red"> {{ msg }} </div> </div> </template> <script> import Child from './Child'; export default { data() { return { msg: '' } }, components: { Child }, methods: { fatherMethod() { console.log('我的父組件中的方法'); this.msg = '我的方法被子組件調(diào)用了'; } } }; </script>
// Child.vue <template> <div style="border: 1px solid black; width: 150px; margin: 10px auto"> 我是子組件 <button @click="childMethod">點(diǎn)擊調(diào)用父組件方法</button> </div> </template> <script> export default { methods: { childMethod() { this.$parent.fatherMethod(); } } }; </script>
在子組件里用
$emit
向父組件觸發(fā)一個(gè)事件,父組件監(jiān)聽這個(gè)事件(推薦使用)// Father.vue <template> <div style="border: 1px solid red; width: 200px; padding: 10px; margin: 0 auto" > 我是父組件 <Child @fatherMethod="fatherMethod"></Child> <div style="color: red"> {{ msg }} </div> </div> </template> <script> import Child from './Child'; export default { data() { return { msg: '' } }, components: { Child }, methods: { fatherMethod() { console.log('我的父組件中的方法'); this.msg = '我的方法被子組件調(diào)用了'; } } }; </script>
子組件可以使用
$emit
觸發(fā)父組件的自定義事件。// Child.vue <template> <div style="border: 1px solid black; width: 150px; margin: 10px auto"> 我是子組件 <button @click="childMethod">點(diǎn)擊調(diào)用父組件方法</button> </div> </template> <script> export default { methods: { childMethod() { // fatherMethod父組件方法 this.$emit('fatherMethod'); } } }; </script>
父組件把方法傳入子組件中,在子組件里直接調(diào)用這個(gè)方法:
// Father.vue <template> <div style="border: 1px solid red; width: 200px; padding: 10px; margin: 0 auto" > 我是父組件 <Child :fatherMethod="fatherMethod"></Child> <div style="color: red"> {{ msg }} </div> </div> </template> <script> import Child from './Child'; export default { data() { return { msg: '' } }, components: { Child }, methods: { fatherMethod() { console.log('我的父組件中的方法'); this.msg = '我的方法被子組件調(diào)用了'; } } }; </script>
父組件可以將事件綁定到子組件標(biāo)簽上,子組件使用
props
接收父組件的事件。// Child.vue <template> <div style="border: 1px solid black; width: 150px; margin: 10px auto"> 我是子組件 <button @click="childMethod">點(diǎn)擊調(diào)用父組件方法</button> </div> </template> <script> export default { props: { fatherMethod: { type: Function, default: null } }, methods: { childMethod() { if (this.fatherMethod) { this.fatherMethod(); } } } }; </script>
以上三種實(shí)現(xiàn)方式效果一致。
調(diào)用方法前:
調(diào)用方法后:
四、總結(jié)
至此,Vue 父子組件之間大部分的操作都涉及到了,我們?cè)诔绦虻拈_發(fā)過(guò)程中對(duì)于該部分內(nèi)容可以游刃有余了。
到此這篇關(guān)于Vue中父子組件通信與事件觸發(fā)的文章就介紹到這了,更多相關(guān)Vue父子組件通信與事件觸發(fā)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue通過(guò)指令(directives)實(shí)現(xiàn)點(diǎn)擊空白處收起下拉框
這篇文章主要介紹了vue通過(guò)指令(directives)實(shí)現(xiàn)點(diǎn)擊空白處收起下拉框,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12spring-cloud-stream的手動(dòng)消息確認(rèn)問(wèn)題
這篇文章主要介紹了spring-cloud-stream的手動(dòng)消息確認(rèn)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05使用vue-cli+webpack搭建vue開發(fā)環(huán)境的方法
這篇文章主要介紹了使用vue-cli+webpack搭建vue開發(fā)環(huán)境的方法,需要的朋友可以參考下2017-12-12vue實(shí)現(xiàn)微信分享鏈接添加動(dòng)態(tài)參數(shù)的方法
這篇文章主要介紹了vue微信分享鏈接添加動(dòng)態(tài)參數(shù)的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-04-04vue項(xiàng)目接入高德地圖點(diǎn)擊地圖獲取經(jīng)緯度以及省市區(qū)功能
這篇文章主要給大家介紹了關(guān)于vue項(xiàng)目接入高德地圖點(diǎn)擊地圖獲取經(jīng)緯度以及省市區(qū)功能的相關(guān)資料,開發(fā)中我們需要地圖定位,就是用戶輸入位置,自動(dòng)定位獲取經(jīng)緯度,需要的朋友可以參考下2023-08-08