element ui dialog 父子组件传值

最近在做课设的时候 用到了Elementui 中的dialog的组件,但在将dialog作为一个子组件的时候,传值出现了问题。当关闭dialog的时候应该怎么传值?

一开始的时候的想法比较简单,就是父组件直接传值来作为子组件的show/hidden的值。

然而这样的问题是当子组件close的时候,他会直接直接改变他的值,但是子组件不能直接改变props的值,因此可以通过子组件触发事件给父组件。

子组件在关闭时候的事件,通过阅读官方文档,我们发现他提供了一个关闭的时候的回调事件。

我们可以通过@close=“相应事件”来写需要给子组件传的事件。

父组件:

<el-button style="float: right; padding: 3px 8px" type="text" @click="report(item)">举报</el-button>
<accuse :accuseitem="accuseitem" :accuseVisible="accuseVisible" @close-dialogStatus="close_dialog"></accuse>
export default {
  name: 'home',
  data () {
    return {
      accuseitem: {},
      accuseVisible: false
  },
  methods: {
   close_dialog (val) {
      this.accuseVisible = false
    },
    report (item) {
      this.accuseitem = item
      this.accuseVisible = true
    }
  }
}

子组件

 <el-dialog
      custom-class="m-dialog"
      :visible.sync="vis"
      width="100%"
      top="0px"
      @close="closeDialog"
      :show-close="true"
      >
    </el-dialog>
export default {
    name: '',
    data () {
        return {
          vis: false
        }
    },
    props: {
        accuseVisible: Boolean,
        accuseitem: Object
    },
    watch: {
        accuseVisible (newValue, oldValue) {
          this.vis = newValue
        }
    },
    methods: {
        closeDialog () {
          this.$emit('close-dialogStatus', true)
        }
    }
}

站在巨人的肩膀上摘苹果:

https://blog.csdn.net/qq_37021554/article/details/85081786

posted @   未月廿三  阅读(3950)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示