日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

埋坑一: vue中子组件调用兄弟组件方法

發布時間:2025/3/8 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 埋坑一: vue中子组件调用兄弟组件方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

小計: 開發中遇到子組件需要調用兄弟組件中的方法,如下寫個小demo記錄下心得,如果你有好的方法,請到評論區域指教

父組件示例代碼:

組件功能解析: 通過$emit獲取子組件事件,通過$ref調用子組件中事件,實現子組件二的click事件調用兄弟組件一中的事件 <template><div><!-- 子組件1 --><son1 ref="borther" :dataFromFather="dataFromFather"></son1><!-- 子組件2 --><son2 @triggerBrotherMethods="triggerBrotherMethods" :dataFromFather="dataFromFather"></son2></div> </template><script> // 引入子組件一 import son1 from './son1' // 引入子組件二 import son2 from './son2'export default {data() {return {dataFromFather: []}},// 注冊子組件components: {son1,son2},methods: {// 子組件2中click事件triggerBrotherMethods() {// 父組件通過$ref調用子組件1中的事件方法this.$refs.borther[0].bortherMethods()},} } </script><style lang="less" scoped> /* .... */ </style>

子組件一

組件功能解析: 加載父組件數據,進行業務操作 <template><!-- 子組件son2 --><div @click="bortherMethods"><!-- 父組件傳值展示 -->{{dataFromFather}}</div> </template><script> export default {data() {return {}},props: ['dataFromFather'],methods: {// 兄弟組件中的按鈕事件bortherMethods() {// 子組件事件方法...},} } </script><style lang="less" scoped> /* .... */ </style>

子組件二:

組件功能解析: 加載父組件數據,通過click事件emit傳給父組件 <template><!-- 子組件son2 --><div @click="triggerBrotherMethods"><!-- 父組件傳值展示 -->{{dataFromFather}}</div> </template><script> export default {data() {return {}},props: ['dataFromFather'],methods: {// 觸發兄弟組件中的按鈕事件triggerBrotherMethods() {this.$emit('clickBrotherBtn', true)},} } </script><style lang="less" scoped> /* .... */ </style>

總結

以上是生活随笔為你收集整理的埋坑一: vue中子组件调用兄弟组件方法的全部內容,希望文章能夠幫你解決所遇到的問題。

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