Vue組件之間的通信你知道多少
Vue組件間通信
vue組件間通信分為以下幾種:
- 父向子傳遞數(shù)據(jù),用自定義屬性
- 子向父傳遞數(shù)據(jù),用自定義事件
- 兄弟(任意)組件間傳遞數(shù)據(jù),用全局事件總線或者消息訂閱與發(fā)布
背吧,面試題要是說讓你簡述一下,就上面這段話拿過來回答唄。下面就介紹一下這幾種通信方式的簡單用法
1.父向子傳遞數(shù)據(jù)
前面已經(jīng)說了,父向子傳遞數(shù)據(jù)用的是自定義屬性
,接下來就讓我們看下代碼是怎么寫的
//這是父組件,Father.vue <template> <div class="father"> <!-- 父組件向子組件傳遞person對象 --> <Son :person="person"/> </div> </template> <script> export default { data (){ return { person:{name:'張三',age:18,sex:'男'} } } } </script>
//這是子組件,Son.vue <template> <div class="son"> <h2>我是子組件</h2> <div> <h4>個(gè)人信息展示</h4> <ul> <li><label>姓名:</label><span>{{person.name}}</span></li> <li><label>年齡:</label><span>{{person.age}}</span></li> <li><label>性別:</label><span>{{person.sex}}</span></li> </ul> </div> </div> </template> <script> export default { //子組件通過props接收父組件傳遞過來的數(shù)據(jù) props:['person'] } </script>
這里題外話,簡單的介紹下props
1.props的大小寫
當(dāng)我們使用自定義屬性傳遞數(shù)據(jù)的時(shí)候,自定屬性的名稱
可能不會(huì)簡單的是一個(gè)單詞,那這個(gè)時(shí)候我們該如何書寫呢?請看如下代碼
//父組件 父組件傳遞了 company-name <Son company-name = "Microsoft"></Son> //子組件 子組件接收時(shí)的寫法 <script> export default { props:['companyName'] } </script>
2.props的兩種寫法
第一種是簡單的寫法
props:['name','age','sex']
第二種是對象形式的寫法
props:{ name:{ type:String, required:true, default:'' }, age:{ type:Number, required:true, default:0 }, sex:String }
這兩種寫法根據(jù)實(shí)際情況來決定,一般的使用第一種就可以滿足需求
3.傳遞動(dòng)態(tài)props
通過v-bind
為組件傳遞動(dòng)態(tài)數(shù)據(jù)類型
<Son :categoryList="categoryList"></Son> <script> export default { data (){ return { //購物車列表數(shù)據(jù) categoryList:[] } } } </script>
2.子向父傳遞數(shù)據(jù)
前面講到,子向父傳遞數(shù)據(jù)需要用到自定義事件
,但是這里通過自定義屬性
也可以實(shí)現(xiàn),我們一起來看一下
//子組件 Son.vue <template> <div class="son"> <button @click="sendMsgForFather">發(fā)送信息</button> </div> </template> <script> export default { props:['getSonMessage'], methods:{ sendMsgForFather(){ this.getSonMessage(`我是子組件,你好啊`) } } } </script>
//父組件 Father.vue <template> <div class="father"> <h1>我是父組件</h1> <Son :getSonMessage="getSonMessage"/> </div> </template> <script> import Son from '@/components/Son.vue' export default { components : { Son }, methods:{ getSonMessage(msg){ console.log(`我收到了子組件傳遞過來的信息:${msg}`); } } } </script>
下面這段代碼是通過自定義事件
實(shí)現(xiàn)的
//子組件 Son.vue <template> <div class="son"> <button @click="sendMsgForFather">發(fā)送信息</button> </div> </template> <script> export default { props:['getSonMessage'], methods:{ //發(fā)送信息給父組件 sendMsgForFather(){ this.$emit('eventN',`我是子組件,hello`) } } } </script>
//父組件 Father.vue <template> <div class="father"> <h1>我是父組件</h1> <Son @eventN="demo"/> </div> </template> <script> import Son from '@/components/Son.vue' export default { components : { Son }, methods:{ demo(msg){ console.log(`觸發(fā)了demo函數(shù),${msg}`); } } } </script>
其實(shí)理解起來還是很簡單的,給子組件上綁定一個(gè)自定義事件,子組件上的自定義事件通過$emit
觸發(fā),而$emit
通過子組件上的按鈕點(diǎn)擊事件來觸發(fā),最終將數(shù)據(jù)發(fā)送給父組件,父組件通過demo函數(shù)拿到并展示數(shù)據(jù),估計(jì)聽完我說的,肯定蒙了。研究一下代碼,自己敲一下就明白了
3.兄弟(任意)組件間的傳值
兄弟組件間的傳值方法比較多,包括我們甚至可以通過逐層使用自定義屬性
和自定義事件
來實(shí)現(xiàn),但代碼看起來可能不是那么舒服,甚至把自己都給繞暈了,我自己也試過,想想這種方法知道就行,效率太低了。接下來就講講目前主流的幾種兄弟組件間傳值的方法
3.1全局事件總線
//main.js中安裝全局事件總線 new Vue({ render: h => h(App), beforeCreate(){ Vue.prototype.$bus = this //安裝全局事件總線 } }).$mount('#app')
//消息發(fā)送方 SendCom.vue <template> <div class="send-container"> <!-- SendCom 向組件 GetMsg 發(fā)送信息,通過$emit觸發(fā)自定義事件--> <button @click="sendMsg">發(fā)送消息</button> </div> </template> <script> export default { methods:{ sendMsg(){ this.$bus.$emit('eventB','hello') } } } </script>
//消息接收方 GetMsg.vue <template> <div class="get-msg-container"> </div> </template> <script> export default { mounted() { console.log(this); this.$bus.$on('eventB', (data) => { console.log(`我是事件接受方Demo2,我收到了數(shù)據(jù)${data}`); }); }, beforeDestroy() { this.$bus.$off('eventB') //在使用完后將事件解綁 } }; </script>
3.2消息訂閱與發(fā)布
消息訂閱與發(fā)布在原生js下實(shí)現(xiàn)比較復(fù)雜,這里使用第三方庫pubsub-js
,通過npm i pubsub-js
安裝。
//消息訂閱者(接收方) Subscribe.vue <template> <div class="subscribe"> </div> </template> <script> import pubsub from 'pubsub-js' export default { mounted(){ this.pubId = pubsub.subscribe('message',(msgName,data)=>{ console.log(`我收到了消息${msgName},內(nèi)容是${data}`); }) }, beforeDestroy(){ pubsub.unsubscribe(this.pubId) } } </script
//消息發(fā)布者(發(fā)送方) Publish.vue <template> <div class="publish"> <button @click="sendMsg">點(diǎn)擊發(fā)送</button> </div> </template> <script> import pubsub from 'pubsub-js' export default { methods:{ sendMsg(){ pubsub.publish('message','這是訂閱的消息') } } } </script>
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
vue計(jì)算屬性無法監(jiān)聽到數(shù)組內(nèi)部變化的解決方案
今天小編就為大家分享一篇vue計(jì)算屬性無法監(jiān)聽到數(shù)組內(nèi)部變化的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11vue3+el-table實(shí)現(xiàn)行列轉(zhuǎn)換
在處理文本數(shù)據(jù)的時(shí)候,我們經(jīng)常會(huì)需要把文本數(shù)據(jù)的行與列進(jìn)行轉(zhuǎn)換操作,這樣更方便查看,本文就詳細(xì)的介紹了vue3+el-table實(shí)現(xiàn)行列轉(zhuǎn)換,感興趣的可以了解一下2021-06-06關(guān)于vue中路由的跳轉(zhuǎn)和參數(shù)傳遞,參數(shù)獲取
這篇文章主要介紹了關(guān)于vue中路由的跳轉(zhuǎn)和參數(shù)傳遞,參數(shù)獲取方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Vue實(shí)現(xiàn)導(dǎo)航欄點(diǎn)擊當(dāng)前標(biāo)簽變色功能
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)導(dǎo)航欄點(diǎn)擊當(dāng)前標(biāo)簽變色功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05詳解Vue 普通對象數(shù)據(jù)更新與 file 對象數(shù)據(jù)更新
本篇文章主要介紹了詳解Vue 普通對象數(shù)據(jù)更新與 file 對象數(shù)據(jù)更新 ,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-04-04vue項(xiàng)目打包部署后默認(rèn)路由不正確的解決方案
這篇文章主要介紹了vue項(xiàng)目打包部署后默認(rèn)路由不正確的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04從零開始在NPM上發(fā)布一個(gè)Vue組件的方法步驟
這篇文章主要介紹了從零開始在NPM上發(fā)布一個(gè)Vue組件的方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12