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>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 中,组件和子组件之间可以通过事件通信。 组件向子组件传递数据,可以通过 props 属性实现。而子组件组件传递数据,则可以通过事件的方式实现。 具体实现方法如下: 1. 组件向子组件传递数据 在组件中,通过 props 属性将数据传递给子组件: ```html <template> <div> <child-component :message="message"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { components: { ChildComponent }, data() { return { message: 'Hello world!' } } } </script> ``` 在子组件中,接收组件传递的数据: ```html <template> <div>{{ message }}</div> </template> <script> export default { props: { message: { type: String, default: '' } } } </script> ``` 2. 子组件组件传递数据 在子组件中,通过 $emit() 方法触发事件,将数据传递给组件: ```html <template> <button @click="sendMessage">发送消息</button> </template> <script> export default { methods: { sendMessage() { this.$emit('send-message', 'Hello world!') } } } </script> ``` 在组件中,通过 v-on 指令监听子组件触发的事件,并在事件处理函数中获取子组件传递的数据: ```html <template> <div> <child-component @send-message="handleMessage"></child-component> </div> </template> <script> import ChildComponent from './ChildComponent.vue' export default { components: { ChildComponent }, methods: { handleMessage(message) { console.log(message) // 输出:Hello world! } } } </script> ``` 以上就是 Vue组件和子组件之间通过事件通信的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值