vue父组件与子组件通信

 在父组件上弹出子组件

父组价代码

<template>
    <div>
        <div class='container'>
            <el-button type='primary' icon='el-icon-create' @click='showAdd'>新增
            </el-button>
            <addDevice @add_dialogStatus='closeAdd' :dialogStatus='addDialogStatus'>
            </addDevice>
        </div>
    </div>
</template>
<script>
//引入组件
import addDevice from '../common/addDevice';//引入子组件
export default {
    components: {
        addDevice  //注册子组件,便于在本页面中使用
    },
    data() {
        return { addDialogStatus: false };
    },
    methods: {
        showAdd() {
            this.addDialogStatus = true;
        },
        closeAdd() {
            this.addDialogStatus = false;
        }
    }
};
</script>

<style scoped>
</style>

子组件代码段

<template>
    <el-dialog title='添加设备' :visible.sync='dialogFormViseble' @close='closeDialog'>
        <el-form ref='form' :model='device_form' style='margin-left: 30px'>
        </el-form>
        <div slot='footer' class='dialog-footer'>
            <el-button @click='dialogFormViseble = false'>取 消</el-button>
            <el-button type='primary' @click='dialogFormViseble = false'>确 定</el-button>
        </div>
    </el-dialog>
</template>
<script>
export default {
    props: ['dialogStatus'],//子组件接受父组件数据
    data() {
        return {
            dialogFormViseble: false
        };
    },
    methods: {
        closeDialog() {
            this.$emit('add_dialogStatus');
        }
    },
    watch: {
        //子组件监听父组件的dialogStatus变化
        dialogStatus(newLv, oldLv) {
            this.dialogFormViseble = newLv;
        }
    }
};
</script>
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值