前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Vue父组件与子组件传递事件/调用事件

Vue父组件与子组件传递事件/调用事件

原创
作者头像
IT工作者
发布2022-02-17 21:10:26
3.1K0
发布2022-02-17 21:10:26
举报
文章被收录于专栏:程序技术知识

父组件:

代码语言:javascript
复制
<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>
子组件:
代码语言:javascript
复制
<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方法。

父组件:

代码语言:javascript
复制
<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>
子组件:
代码语言:javascript
复制
<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是否存在,是的话直接执行这个方法。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档