通过弹窗组件,谈父子组件传参

通常在构建页面时会将弹窗封装为一个组件方便后期维护,下面我浅谈一下弹窗组件开关标识传递的几种方法。

(1).父传子,子传父将弹窗标识flag传递;

        父组件:

<template>
  <div class="father">
    <sonDialog :dialogFlag="dialogFlag" @closeDialog="closeDialog"></sonDialog>
  </div>
</template>
<script>
import sonDialog from './sonDialog.vue'
export default {
  data() {
    return {
        dialogFlag:false
    }
  },
  methods:{
    closeDialog(){
      this.dialogFlag= false
    },
  }
}
</script>

        子组件:

<template>
  <el-dialog title="提示" :visible.sync="dialogFlag">
    <span>
        注意:props接受父组件的dialogFlag,通过$emit传递closeDialog方法给父组件关闭弹窗
    </span>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary">确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
export default {
  props:["dialogFlag"],
  methods:{
    closeDialog(){
      this.$emit("closeDialog", false);
    }
  }
};
</script>

<style>
</style>

(2).将弹窗开关的方法都定义在子组件中,在需要打开弹窗时,使用ref属性直接在父组件中调用子组件的方法:

        父组件

<template>
  <div class="father">
    <el-button type="primary" @click="openDialog"> 打开弹窗 </el-button
    <sonDialog ref="child""></sonDialog>
  </div>
</template>
<script>
import sonDialog from './sonDialog.vue'
export default {
  methods:{
    openDialog(){
      this.$refs.child.openDialog();
    },
  }
}
</script>

        子组件

<template>
  <el-dialog title="提示" :visible.sync="dialogFlag">
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary">确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
export default {
  data(){
     return{
        dialogFlag:false
    }
  },
  methods:{
    openDialog(){
      this.dialogFlag = true;
    },
    closeDialog(){
      this.dialogFlag = false;
    }
  }
};
</script>

<style>
</style>

(3).将弹窗标识flag定义在一个复杂数据类型对象中dialogFlag{flag:false}传递给子组件,直接在子组件中操作标识控制弹窗的显示隐藏:

        父组件

<template>
  <div class="father">
    <sonDialog :dialogFlag="dialogFlag"></sonDialog>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="openDialog">确 定</el-button>
    </span>
  </div>
</template>
<script>
import sonDialog from './sonDialog.vue'
export default {
  data() {
    return {
        dialogFlag:{flag:false}
    }
  },
  methods{        
    openDialog(){
        this.dialog.flag = true;
    }
  }
}
</script>

        子组件

<template>
  <el-dialog
    :visible.sync="dialogFlag.flag"
  >
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary">确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
export default {
  props:["dialogFlag"],
  methods:{
    cancel(){
      this.dialogFlag.flag = false
    },
  }
};
</script>

<style>
</style>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值