日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

总结vue中父向子,子向父以及兄弟之间通信的几种方式

發布時間:2025/6/17 vue 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 总结vue中父向子,子向父以及兄弟之间通信的几种方式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

子向父方式1:通過props,如例子中子組件test1.vue向父組件App.vue傳值

  App.vue代碼

<template><div id="app"><test1 :parfn="parfn"></test1></div> </template><script> import test1 from '@/components/test1.vue' export default {name: 'App',data () {return {msg: 'parent'}},components: {test1},methods: {parfn: function (a) {alert(a)}} } </script>

  test1.vue代碼

<template><div>i am test1</div> </template><script> export default {data () {return {msg: 'test1'}},props: {parfn: {type: Function}},created: function () {this.parfn(this.msg)} } </script>

效果圖

?

子向父方式2:通過$parent

  App.vue代碼:  

<template><div id="app"><test1></test1></div> </template><script> import test1 from '@/components/test1.vue' export default {name: 'App',data () {return {msg: 'parent'}},components: {test1},methods: {parfn: function (a) {alert(a)}} } </script>

  test1.vue代碼: 

<template><div>i am test1</div> </template><script> export default {data () {return {msg: 'test1'}},created: function () {this.$parent.parfn(this.msg)} } </script>

效果圖:

子向父方式3:通過emit

  App.vue代碼

<template><div id="app"><test1 @myfn="parfn"></test1></div> </template><script> import test1 from '@/components/test1.vue' export default {name: 'App',data () {return {msg: 'parent'}},components: {test1},methods: {parfn: function (a) {alert(a)}} } </script>

  test1.vue代碼: 

<template><div>i am test1</div> </template><script> export default {data () {return {msg: 'test1'}},mounted: function () {this.$emit('myfn', this.msg)} } </script>

效果圖:

?

父向子傳值方式1:通過props

  App.vue代碼: 

<template><div id="app"><test1 :msg="msg"></test1></div> </template><script> import test1 from '@/components/test1.vue' export default {name: 'App',data () {return {msg: 'parent'}},components: {test1} } </script>

  test1.vue代碼:

<template><div>i am test1</div> </template><script> export default {props: ['msg'],created: function () {alert(this.msg)} } </script>

  效果圖:

  

父向子方式2:通過$children

  App.vue代碼:  

<template><div id="app"><test1></test1></div> </template><script> import test1 from '@/components/test1.vue' export default {name: 'App',data () {return {msg: 'parent'}},mounted: function () {this.$children[0].childfn(this.msg)},components: {test1} }

  test1.vue代碼  

<template><div>i am test1</div> </template><script> export default {methods: {childfn: function (a) {alert(a)}} } </script>

  效果圖:

  

父向子方式3:通過ref

  App.vue代碼: 

<template><div id="app"><test1 ref="mychild"></test1></div> </template><script> import test1 from '@/components/test1.vue' export default {name: 'App',data () {return {msg: 'parent'}},mounted: function () {this.$refs.mychild.childfn(this.msg)},components: {test1} } </script>

  test1.vue代碼: 

<template><div>i am test1</div> </template><script> export default {methods: {childfn: function (a) {alert(a)}} } </script>

  效果圖:

  

?兄弟間傳值方式1:通過事件車,例如App.vue組件中兩個兄弟組件test1.vue傳值給test2.vue

  步驟1:在外部如assets下創建bus.js 

// bus.js import Vue from 'vue' export default new Vue()

  步驟2:組件中引入相互傳值的兄弟組件,如App.vue中test1組件傳值給test2組件 

<!--App.vue--> <template><div id="app"><test1></test1><test2></test2></div> </template><script> import test1 from '@/components/test1.vue' import test2 from '@/components/test2.vue' export default {name: 'App',components: {test1, test2} } </script>

  步驟3:test1組件中引入bus,通過$emit發送事件 

<template><div>test1<button @click="send">點擊發送test1數據給test2</button></div> </template><script> import bus from '@/assets/bus' export default {data () {return {msg: 'test1數據111'}},methods: {send: function () {bus.$emit('myfn', this.msg)}} } </script>

  步驟4:test2組件中引入bus,通過$on接收事件

<template><div>i am test2,接收過來的數據為:{{msg}}</div> </template><script> import bus from '@/assets/bus' export default {data () {return {msg: ''}},created: function () {bus.$on('myfn', msg => {this.msg = msg})} } </script>

  效果圖:

兄弟間傳值方式2:子組件傳數據給父,父再傳給另一個兄弟組件,例如App.vue組件中兩個兄弟組件test1.vue傳值給test2.vue

  步驟1:test1組件中通過$emit傳遞數據給父組件App.vue 

<template><div>test1<button @click="send">點擊發送test1數據給test2</button></div> </template><script> export default {data () {return {msg: 'test1數據111'}},methods: {send: function () {this.$emit('myfn', this.msg)}} } </script>

  步驟2:父組件App.vue中,通過v-bind綁定個屬性傳遞給另一個子組件test2.vue

<!--App.vue--> <template><div id="app"><test1 @myfn="getmsg"></test1><test2 :msg="msg"></test2></div> </template><script> import test1 from '@/components/test1.vue' import test2 from '@/components/test2.vue' export default {name: 'App',data () {return {msg: ''}},methods: {getmsg: function (msg) {this.msg = msg}},components: {test1, test2} } </script>

  步驟3:test2.vue組件通過props接收父組件傳遞過來的參數 

<template><div>i am test2,接收過來的數據為:{{msg}}</div> </template><script> export default {props: ['msg'] } </script>

  效果圖:

  

?

轉載于:https://www.cnblogs.com/hesj/p/10568869.html

總結

以上是生活随笔為你收集整理的总结vue中父向子,子向父以及兄弟之间通信的几种方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日日骚一区二区 | 国产精品xxxxx| 亚洲美女综合网 | 美女狠狠干 | 亚洲爆爽av| 免费中文字幕日韩 | 在线视频精品 | av中文资源 | 波多野结衣一二区 | 日本不卡在线观看 | 亚洲午夜av久久乱码 | 网站在线播放 | 奇米色影视 | 欧美一级性生活视频 | 国产熟女精品视频 | 亚洲手机视频 | 成人毛片视频免费看 | 国产高清视频在线播放 | 日韩一区二区三区在线播放 | 色婷婷av一区二区三区麻豆综合 | 天天摸天天碰 | 精品在线免费视频 | 噜噜噜在线视频 | 亚洲妇女无套内射精 | 午夜激情在线观看 | 亚洲美免无码中文字幕在线 | 在线免费看91 | 日本黄视频在线观看 | 日韩精品在线观看网站 | 天堂在线国产 | 自拍偷拍免费 | 成人在线免费看视频 | 欧美天天性 | 欧美色综合网站 | 人妻熟女一区 | 人人草人人干 | 亚洲高清色 | jizz一区| 欧美亚洲影院 | 在线观看 一区 | 国产盗摄精品一区二区酒店 | 羞羞动态图 | 噼里啪啦免费看 | 久久精品午夜 | wwwxx日本| 公肉吊粗大爽色翁浪妇视频 | 朋友人妻少妇精品系列 | 涩里番在线观看 | 欧美人与性动交g欧美精器 国产在线视频91 | 亚洲狼人综合网 | 久久国产精品99久久人人澡 | 91av在| 婷婷激情丁香 | 国内自拍真实伦在线观看 | 亚洲欧美一区二区三区情侣bbw | 欧美一区二区三区成人久久片 | 欧美性综合 | 久久亚洲国产 | 亚洲一区二区三区四区五区六区 | 天天干夜夜玩 | 丰满人妻一区二区三区免费 | 日本久久久久久久久 | 片集网| 中文字幕乱码视频 | 欧美午夜一区 | 国产浮力第一页 | 中国免费看的片 | 中文字幕 自拍 | 国产综合精品在线 | 性猛交xxxx乱大交孕妇印度 | 人妻夜夜爽天天爽 | 国产高潮国产高潮久久久 | 久久久丁香| 久久影院一区二区 | 韩国三级中文字幕hd浴缸戏 | 最新亚洲精品 | 91在线精品入口 | 波多野结衣视频观看 | 亚a在线| 深夜福利免费视频 | 看片网站在线观看 | av无码精品一区二区三区宅噜噜 | 男人资源网站 | 久久婷婷国产 | 中国1级毛片| 韩国电影一区 | 少妇一级淫片免费放2 | 一级大片在线观看 | 日韩女同互慰一区二区 | 51精品| 国产亚洲av在线 | 杏导航aⅴ福利网站 | 天堂在线播放 | 精品1卡二卡三卡四卡老狼 日韩三级网 | 国产毛片欧美毛片久久久 | 精品国产一区二区在线观看 | 国产精品成人99一区无码 | 欧洲免费av | 在线免费三级 |