ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

vue 子组件触发父组件方法的两种方式

2021-03-08 16:01:55  阅读:185  来源: 互联网

标签:触发 vue title content childs modal 组件 div


https://www.cnblogs.com/menggang89-chen/p/12021400.html

 

第一种方法:

如下:通过this.emit()来触发父组件的方法。具体就是子组件触发emit()来触发父组件的方法。具体就是子组件触发emit()来触发父组件的方法。具体就是子组件触发emit绑定的事件watchChild,然后父组件监听watchChild,一旦watchChild被触发便会触发父组件的parentReceive方法。
父组件:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 <template> <div> <div @click="openChild">弹弹弹,弹出子组件</div> <childs ref="alert" @watchChild="parentReceive"></childs<!--监听子组件绑定的方法--> </div> </template>    <script> import childs from '../components/modal/Alert' export default{     data(){         return {             msg: '我是父组件的msg!!!'         }     },     components:{         childs     },     methods:{         parentReceive(message){             alert(message);         },         openChild(){             this.$refs.alert.open('我是子组件','子组件内容!!!!!');         }     } } </script>    <style> </style>

  

子组件:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 <template>   <modal :show='show' ref="modal">     <div slot="title">{{title}}</div>     <div slot="content">{{content}}</div>     <div slot="buttons" class="modal-buttons">       <span class="modal-button" @click="_onClick()">确定</span>     </div>   </modal> </template>    <script> import Modal from './Modal'    export default {   props: {     show: {       type: Boolean,       default: false     },     okText: {       type: String,       default: '确定'     },   },   data(){     return {         title: '',         content: ''     }   },   components: {     Modal   },   methods: {     open (title, content) {         this.title = title         this.content = content       this.$refs.modal.open()     },     close () {         this.title = ''         this.content = ''             this.$refs.modal.close()     },         _onClick(){             this.close();             this.$emit('watchChild','我是从子组件传过来的内容!!!');    //触发$emit绑定的watchChild方法         }   } } </script>

  

第二种方法:

在子组件props中定义属性onOK,类型为function,然后在父组件中把要触发的方法名传递给onOK属性,最后在子组件中判断onOk是否存在,是的话直接执行这个方法。
父组件:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 <template> <div> <div @click="openChild">弹弹弹,弹出子组件</div> <childs ref="alert" :on-ok="parentReceive"></childs>  <!--把父组件的方法名传给子组件的onOK属性--> </div> </template>    <script> import childs from '../components/modal/Alert' export default{     data(){         return {             msg: '我是父组件的msg!!!'         }     },     components:{         childs     },     methods:{         parentReceive(message){             alert(message);         },         openChild(){             this.$refs.alert.open('我是子组件','子组件内容!!!!!');         }     } } </script>    <style> </style>

  

子组件:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 <template>   <modal :show='show' ref="modal">     <div slot="title">{{title}}</div>     <div slot="content">{{content}}</div>     <div slot="buttons" class="modal-buttons">       <span class="modal-button" @click="_onClick()">确定</span>     </div>   </modal> </template>    <script> import Modal from './Modal'    export default {   props: {     show: {       type: Boolean,       default: false     },     okText: {       type: String,       default: '确定'     },     onOk: {    //定义onOK属性       type: Function     }   },   data(){     return {         title: '',         content: ''     }   },   components: {     Modal   },   methods: {     open (title, content) {         this.title = title         this.content = content       this.$refs.modal.open()     },     close () {         this.title = ''         this.content = ''             this.$refs.modal.close()     },         _onClick(){             this.close();             if(this.onOk){                 this.onOk('我是子组件传递过来的数据!!!!');             }         }   } } </script>

标签:触发,vue,title,content,childs,modal,组件,div
来源: https://www.cnblogs.com/gzhbk/p/14500182.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有