打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
vue 子组件触发父组件方法的两种方式
父组件:

<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>
子组件:

<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>
第一种方法:

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

父组件:

<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>
子组件:

<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>
第二种方法:

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

效果:


--------------------- 
作者:yemuxia_sinian 
来源:CSDN 
原文:https://blog.csdn.net/yemuxia_sinian/article/details/80534296 
版权声明:本文为博主原创文章,转载请附上博文链接!
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Vue 在父(子)组件引用其子(父)组件方法和属性
Vue3.x 从零开始(五)—— Router + Vuex + TypeScript 实战演练(上)
vue.js2.0父组件点击触发子组件方法
vue3 teleport的使用案例详解
Vue.js - Day4
Vue.js
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服