父组件中使用element组件的dialog 点击无响应或者再次点击无效问题

方法一:抽离组件后 如果我们点击按钮弹窗子组件的弹窗使之显示在父组件中

那么就要更改子组件的dialogVisible值,要使用ref 关联子组件 这样我们才可以更改其值  话不多说,直接上代码

如果不将子组件的dialogVisible传给父组件 那么这个弹框很可能出现点击没有反应,

 

<el-dialog
  title="提示"
  :visible.sync="dialogVisible"
  width="30%"
  :before-close="handleClose">
  <span>这是一段信息</span>
</el-dialog>

<script>
  export default {
    name:"upload",
    data() {
      return {
        dialogVisible: false
      };
    },
    methods: {
      handleClose(done) {
        this.$confirm('确认关闭?')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      }
    }
  };
</script>

 

<template>
  <div id="App"> 
    <el-button @click="showUpload">上传文件</el-button>
    <upload ref="upload"></upload>

  </div>
</template>
 
<script>
import upload from "../src/components/upload.vue"
  export default {
    name: 'App',
    components:{
      upload
    },
    data () {
      return {
        
      }
    },
    methods: {
      showUpload(){
        this.$refs.upload.dialogVisible=true
      },
    }
  }  
</script>
 
<style scoped>
  .div-label {
    padding: 20px 0;
    width: 100%;
  }
</style>

方法二:我们分别用各自的组件管理各自的变量和对象,推荐方法二  完整代码如下

<template>
  <el-dialog
    title="提示"
    :visible.sync="dialogVisible"
    width="500px"
    :before-close="handleClose"
  >
    <el-card>
      <!-- 下载数据模板 -->
      <div class="div-label">
        <label
          >上传文件之前请先下载<span style="font-weight: bold"
            >数据模板</span
          ></label
        >
      </div>
      <el-button
        @click="download"
        class="el-button-color add-button-box"
        size="medium"
        type="primary"
      >
        <i class="el-icon-download el-icon--right"></i>下载数据模板
      </el-button>
      <!-- 上传文件 -->
      <div class="div-label">
        <label
          >上传文件,仅支持<span style="font-weight: bold">.xlsx</span
          >格式的Excel文件,且大小不超过<span
            style="font-weight: bold; color: red"
            >10MB</span
          ></label
        >
      </div>
      <el-upload
        class="upload-demo"
        ref="upload"
        action=""
        accept=".xlsx"
        :limit="1"
        :file-list="fileList"
        :auto-upload="false"
        :on-exceed="handleExceed"
        :on-preview="handlePreview"
        :on-change="handleChange"
        :on-remove="handleRemove"
        :before-upload="beforeUpload"
        :http-request="httpRequest"
        :on-success="handleSuccess"
        :on-error="handleError"
      >
        <el-button slot="trigger" size="medium" type="primary"
          >选取文件</el-button
        >
        <el-button
          style="margin-left: 10px"
          size="medium"
          type="success"
          @click="submitUpload"
          >上传到服务器</el-button
        >
      </el-upload>
    </el-card>
  </el-dialog>
</template>

<script>
export default {
  name: "x-upload",
  data() {
    return {
      dialogVisible: false,
      fileList: [],
    };
  },
  props:{
    dialogShow:{
      type:Boolean,
      default:false
    },
  },
  watch:{
    // 这里监听dialogShow对象的变化
    dialogShow:{
      deep:true,
      handler(val){
        if(val){
          this.dialogVisible=true;
        }
      }
    }
  },
  methods: {
    handleClose(done) {
      this.$confirm("确认关闭?")
        .then((_) => {
          done();
          // this.dialogVisible=false
          this.$emit("update:dialogShow",false)
        })
        .catch((_) => {});
        
    },
    // 下载本地文件
    download() {
      window.location.href = "http://localhost:8080/static/test.xlsx";
    },

    // 超过文件上传最大个数
    handleExceed(files, fileList) {
      this.$message.warning("很抱歉当前支持最大上传文件个数为 1 个!");
    },
    // 文件上传到服务器之前的文件校验
    beforeUpload(file) {
      const extension = file.name.substring(file.name.lastIndexOf(".") + 1);
      const size = file.size / 1024 / 1024;
      if (extension !== "xlsx") {
        // 校验文件格式
        this.$message.warning("只支持上传后缀名为.xlsx的Excel文件");
      }
      if (size > 10) {
        // 校验文件大小
        this.$message.warning("文件大小不能超过10MB");
      }
    },
    // 文件状态改变
    handleChange(file, fileList) {
      if (file) {
        this.fileList = fileList.slice(-3);
      }
    },
    // 文件删除时
    handleRemove(file, fileList) {
      console.log(file, fileList);
      this.fileList = []; // 文件列表置空
    },
    // 点击文件列表中已上传的文件时的钩子
    handlePreview(file) {
      console.log(file);
    },
    // 文件上传成功
    handleSuccess(response, file, fileList) {
      console.log("-------handleSuccess-------");
      console.log(response);
      this.$message.success("文件 [" + file.name + "] 上传成功");
    },
    // 文件上传失败
    handleError(err, file, fileList) {
      console.log("-------handleError-------");
      console.log(err);
      this.$message.error("文件上传失败");
    },
    // 覆盖默认的上传行为,可以自定义上传的实现
    httpRequest(param) {
      console.log(param);
      const fileObj = param.file; // 获取file文件
      const formData = new FormData(); // FormData对象
      formData.append("file", fileObj); // file封装到FormData里
      // 请求后台上传数据的接口
      this.$http
        .post("/upload", formData, {
          headers: { "Content-Type": "multipart/form-data" },
          // url: '/upload',
          // data: formData
          // method: 'post'
        })
        .then(
          (res) => {
            console.log("-------res--------");
            console.log(res);
            if (res.data.meta.status === "200") {
              this.$message.success(res.data.meta.msg);
              // 清空文件列表
              this.fileList = [];
            } else {
              this.$message.error(res.data.meta.msg);
            }
          },
          (err) => {
            console.log("-------err--------");
            console.log(err);
            this.$message.error(
              "上传文件内容有问题,请保证上传文件字段均不为空且正确"
            );
          }
        );
    },
    // 上传文件
    submitUpload() {
      if (this.fileList.length === 0) {
        // 上传文件列表为空
        this.$message.warning("请选择一个上传文件");
      } else {
        // 执行上传操作
        this.$refs.upload.submit();
      }
    },
  },
};
</script>

<style>
</style>
<template>
  <div id="App"> 
    <el-button @click="showDia">上传文件</el-button>
    <!-- <el-button type="text" @click="showUpload">点击打开 Dialog</el-button> -->
    <!-- 上传文件弹窗 -->
    <x-upload :dialog-show.sync="dialogShow"></x-upload>
    <!-- <el-button type="text" @click="dialogVisible = true">点击打开 Dialog</el-button>
    <upload v-if="seen"></upload> -->
  </div>
</template>
 
<script>
import XUpload from "../src/components/x-upload.vue"
  export default {
    name: 'App',
    components:{
      XUpload
    },
    data () {
      return {
        dialogShow:false,
        seen:false
        
      }
    },
    methods: {
      // showUpload(){
      //   this.$refs.upload.dialogVisible=true
      // },
      // 控制上传文件弹窗
      showDia(){
        this.dialogShow=true
      }
    }
  }  
</script>
 
<style scoped>
  .div-label {
    padding: 20px 0;
    width: 100%;
  }
</style>

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
首先,你需要在组件引入 Dialog 弹框组件,并在组件的 template 添加一个触发显示弹框的按钮。例如: ```vue <template> <div> <button @click="showDialog">显示弹框</button> <Dialog v-model="dialogVisible"></Dialog> </div> </template> ``` 在上面的代码,我们添加了一个按钮,当用户点击该按钮时,会触发 showDialog 方法。同时,我们引入了一个名为 Dialog组件,并将它的显示与隐藏与一个名为 dialogVisible 的变量绑定了起来。 接下来,在组件的 script ,我们需要定义 showDialog 方法以及 dialogVisible 变量。例如: ```vue <script> import Dialog from './Dialog.vue' export default { components: { Dialog }, data() { return { dialogVisible: false } }, methods: { showDialog() { this.dialogVisible = true } } } </script> ``` 在上面的代码,我们首先引入了 Dialog 组件,然后在 data 定义了 dialogVisible 变量,并将其默认设置为 false。接着,我们定义了一个名为 showDialog方法,该方法dialogVisible 设置为 true,从而显示 Dialog 弹框组件。 最后,我们需要在 Dialog 弹框组件添加一个关闭按钮,并将其与 dialogVisible 变量绑定起来。例如: ```vue <template> <div> <div class="dialog-mask" v-if="visible" @click="close"></div> <div class="dialog-wrapper" v-if="visible"> <div class="dialog-content"> <div class="dialog-header"> <h3 class="dialog-title">Dialog 标题</h3> <button class="dialog-close-btn" @click="close">×</button> </div> <div class="dialog-body"> <p>Dialog 内容</p> </div> </div> </div> </div> </template> <script> export default { props: { visible: { type: Boolean, required: true } }, methods: { close() { this.$emit('update:visible', false) } } } </script> ``` 在上面的代码,我们添加了一个名为 close 的方法,该方法会将 dialogVisible 设置为 false,并通过 $emit 方法将更新后的传递给组件。同时,我们在弹框组件添加了一个关闭按钮,并在按钮的 click 事件调用 close 方法。 这样,当用户点击组件的按钮时,就会触发 showDialog 方法,从而显示 Dialog 弹框组件。当用户点击弹框的关闭按钮时,会触发 close 方法,从而关闭弹框。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值