国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

vue中子組件調用兄弟組件方法

2024-05-06 15:32:55
字體:
來源:轉載
供稿:網友

小計: 開發中遇到子組件需要調用兄弟組件中的方法,如下寫個小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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持錯新站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 东兰县| 称多县| 台东市| 曲阳县| 黄龙县| 阿拉善盟| 建平县| 平湖市| 景德镇市| 塔城市| 左云县| 古田县| 灌南县| 武安市| 杭锦后旗| 齐齐哈尔市| 孝义市| 万载县| 雷州市| 兴化市| 兴宁市| 车险| 凤山县| 郯城县| 汉寿县| 湖南省| 涿鹿县| 贵阳市| 濮阳县| 青川县| 河东区| 高安市| 涡阳县| 达州市| 勐海县| 马山县| 汉川市| 佛学| 汶上县| 当雄县| 南汇区|