Vue Element-UI:将对话框作为子组件

点击‘更新’按钮,弹出子组件uploadDialog,它是一个上传图片的组件
在这里插入图片描述
在这里插入图片描述
父子组件:

<template>
  <div>
    <el-card shadow="hover">
      <h2 class="wx">微信支付</h2>
      <hr />
      <el-form ref="myForm" :model="myForm" label-width="auto">
        <el-form-item label="二维码 : ">
          <el-image style="width: 100px; height: 100px" :src="url" :fit="fit">
            <div slot="placeholder" class="image-slot">
              加载中<span class="dot">...</span>
            </div>
          </el-image>
          <el-link type="warning" @click="dialogVisible = true" class="link"
            >更新</el-link
          >
        </el-form-item>
        <el-form-item label="第一步 :">
          <el-input
            type="textarea"
            :rows="txRows"
            placeholder="请输入内容"
            v-model="myForm.txOne"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="第二步 :">
          <el-input
            type="textarea"
            :rows="txRows"
            placeholder="请输入内容"
            v-model="myForm.txTwo"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="第三步 :">
          <el-input
            type="textarea"
            :rows="txRows"
            placeholder="请输入内容"
            v-model="myForm.txThree"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-row>
            <el-col :offset="19">
              <el-link type="warning" @click="save" class="saveLink"
                >保存所有</el-link
              >
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
    </el-card>
    <upload-dialog :hzyVisible="dialogVisible" v-on:childShow="childShowFun" />
  </div>
</template>
<script>
import uploadDialog from './uploadDialog'

import { getPay } from '@/api/pay'

export default {
  name: 'wechat',
  components: { uploadDialog },
  created() {
    this._getPay()
  },
  data() {
    return {
      dialogVisible: false,
      txRows: 2,
      myForm: {
        txOne: '',
        txTwo: '',
        txThree: ''
      },
      url: '',
      fit: 'fill'
    }
  },
  methods: {
    childShowFun() {
      this.dialogVisible = false
    },
    _getPay() {
      const type = { type: 'wx' }
      getPay(type)
        .then(res => {
          this.url = res.data.url
          this.myForm.txOne = res.data.txOne
          this.myForm.txTwo = res.data.txTwo
          this.myForm.txThree = res.data.txThree
        })
        .catch(error => {
          this.$message.error(error.message)
        })
    },
    save() {
      this.$message.success('保存成功')
    }
  }
}
</script>
<style>
.link {
  margin-left: 10px;
  margin-bottom: 5px;
}
.wx {
  font-size: 15px;
  text-align: center;
}
</style>

对话框

<template>
  <div>
    <el-dialog
      title="提示"
      :visible.sync="hzyVisible"
      width="30%"
      :before-close="handleClose"
    >
      <div>
        abc
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelDialogBtn">取 消</el-button>
        <el-button type="primary" @click="submitDialogBtn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'uploadDialog',
  props: {
    hzyVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      boolShow: this.hzyVisible
    }
  },
  methods: {
    cancelDialogBtn() {
      this.$emit('childShow')
    },
    submitDialogBtn() {
      this.$emit('childShow')
    },
    handleClose(done) {
      this.$confirm('确认关闭?')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    }
  }
}
</script>

重点要知道的是:
子组件uploadDialog中的this.$emit(‘childShow’)和父组件中的v-on是一一对应的

    <upload-dialog :hzyVisible="dialogVisible" v-on:childShow="childShowFun" />

如果没有v-on方法,调用一定报错:
vue.runtime.esm.js:619 [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "hzyVisible"

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Vue中使用element-ui的el-dialog对话框并实现拖动功能,可以使用vue-draggable-resizable插件来实现。 1. 安装插件 使用npm或者yarn进行安装。 ``` npm install vue-draggable-resizable --save ``` 或者 ``` yarn add vue-draggable-resizable ``` 2. 引入插件 在Vue组件中引入vue-draggable-resizable插件,并注册为全局组件。 ```javascript import Vue from 'vue' import VueDraggableResizable from 'vue-draggable-resizable' import 'vue-draggable-resizable/dist/VueDraggableResizable.css' Vue.component('vue-draggable-resizable', VueDraggableResizable) ``` 3. 使用插件 在el-dialog组件中嵌套vue-draggable-resizable组件,并设置对话框的宽度和高度。 ```html <template> <div> <el-button type="primary" @click="dialogVisible = true">打开对话框</el-button> <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose"> <vue-draggable-resizable :w="dialogWidth" :h="dialogHeight"> <p>这是一个可以拖动的对话框</p> </vue-draggable-resizable> </el-dialog> </div> </template> <script> export default { data() { return { dialogVisible: false, dialogWidth: 400, dialogHeight: 300 } }, methods: { handleClose(done) { this.dialogVisible = false done() } } } </script> ``` 在这个示例中,我们将vue-draggable-resizable组件嵌套在el-dialog组件中,并设置了对话框的宽度和高度。我们还定义了一个handleClose方法来处理对话框关闭的事件。 现在你可以在Vue中使用element-ui的el-dialog对话框并实现拖动功能了。注意:vue-draggable-resizable插件的样式可能需要根据自己的需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东方文艺复兴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值