vue 引用自定义Dialog组件问题

本文介绍了如何在Vue.js项目中创建一个可复用的自定义Dialog组件,用于展示文件列表和历史记录。通过监听`visible`属性来控制Dialog的显示隐藏,并在`sURL`改变时动态加载数据。组件内部包含了文件列表和历史版本两个Tab,每个Tab下都有一个表格展示数据并提供下载操作。在父组件中,通过传递`url`和`visible`属性来调用和关闭Dialog。
摘要由CSDN通过智能技术生成

View中引用自定义Dialog组件

需求: 在项目中,有时候可能在不同画面需要完成同一功能,比如示例文件列表查看功能,系统上传文件,需要查看文件列表,以及文件历史记录

话不多说,上图

 

 

 这个查看文件的Dialog需要在系统中的很多地方调用,开发过程中不可能在每个地方都定义一遍,那么,我们可能使用自定义控件形式,代码如下

<template>

  <el-dialog title="查看文件" :visible.sync="innerVisible" class="form-dialog" center @closed="onClosed">

    <el-tabs v-model="activeName" @tab-click="handleClick">

      <el-tab-pane label="文件列表" name="fileList">

        <el-table

          v-loading="listLoading"

          size="small"

          :data="list"

          style="width: 100%"

          element-loading-text="Loading"

          border

          stripe

          fit

          highlight-current-row

        >

          <el-table-column label="序号" align="center">

            <template slot-scope="scope">

              {{ scope.$index + 1 }}

            </template>

          </el-table-column>

          <el-table-column label="文件名" align="center">

            <template slot-scope="scope">

              {{ scope.row.name }}

            </template>

          </el-table-column>

          <el-table-column label="文件大小(KB)" align="center">

            <template slot-scope="scope">

              <span>{{ scope.row.size }}</span>

            </template>

          </el-table-column>

          <el-table-column label="操作" align="center">

            <template slot-scope="scope">

              <el-button size="mini" @click="downLoadFile(scope.row)">下载</el-button>

            </template>

          </el-table-column>

        </el-table>

      </el-tab-pane>

      <el-tab-pane label="历史版本" name="history">

        <el-table

          v-loading="historyListLoading"

          size="small"

          :data="historyList"

          style="width: 100%"

          element-loading-text="Loading"

          border

          stripe

          fit

          highlight-current-row

        >

          <el-table-column label="序号" align="center">

            <template slot-scope="scope">

              {{ scope.$index + 1 }}

            </template>

          </el-table-column>

          <el-table-column label="时间" align="center">

            <template slot-scope="scope">

              {{ scope.row.Date | parseTime('{y}-{m}-{d} {h}:{i}') }}

            </template>

          </el-table-column>

          <el-table-column label="备注" align="center">

            <template slot-scope="scope">

              <span>{{ scope.row.Remark }}</span>

            </template>

          </el-table-column>

          <el-table-column label="操作" align="center">

            <template slot-scope="scope">

              <el-button size="mini" @click="downLoadVersionFile(scope.row)">下载</el-button>

            </template>

          </el-table-column>

        </el-table>

      </el-tab-pane>

    </el-tabs>

    <div slot="footer" class="dialog-footer">

      <el-button type="primary" @click="innerVisible = false">确定</el-button>

    </div>

  </el-dialog>

</template>



<script>

import { fileDownload, getFileList, getFileHistoryList, versionFileDownload } from '@/api/common/common'

import { getToken } from '@/utils/auth'

import { parseTime, downloadFile } from '@/utils'

export default {

  name: 'FileListDialog',

  filters: {

    parseTime(time, cFormat) {

      return parseTime(time, cFormat)

    }

  },

  components: {

  },

  props: {

    visible: {

      type: Boolean,

      default: false

    },

    url: {

      type: String,

      default: ''

    }

  },

  data() {

    return {

      token: getToken(),

      list: null,

      historyList: null,

      listLoading: true,

      historyListLoading: true,

      innerVisible: this.visible,

      sURL: '',

      activeName: 'fileList'

    }

  },

  watch: {

    visible(val, oldVal) {

      if (val === oldVal) {

        return

      }

      this.innerVisible = val

    },

    innerVisible(val, oldVal) {

      if (val === oldVal) {

        return

      }

      this.$emit('update:visible', val)

    },

    sURL(val, oldVal) {

      if (val === oldVal) {

        return

      }

      this.activeName = 'fileList'

      // 获取数据

      this.getList()

    }

  },

  mounted() {},

  updated() {

    this.sURL = this.url

    this.dVisible = this.dialogFormVisible

  },

  methods: {

    handleClick(tab, event) {

      switch (tab.name) {

        case 'fileList':

          this.getList()

          break

        case 'history':

          this.getHistory()

          break

      }

    },

    getList() {

      this.listLoading = true

      var param = {

        url: this.sURL,

        token: getToken()

      }

      getFileList(param).then(response => {

        this.list = response.data

        this.listLoading = false

      })

    },

    getHistory() {

      this.historyListLoading = true

      var param = {

        url: this.sURL,

        token: getToken()

      }

      getFileHistoryList(param).then(response => {

        this.historyList = response.data

        this.historyListLoading = false

      })

    },

    onClosed() {

      this.sId = ''

    },

    downLoadFile(row) {

      var param = {

        token: getToken(),

        url: row.url

      }

      fileDownload(param).then(res => {

        if (res.code === 20000) {

          downloadFile(res.data.name, res.data.dataStr)

        }

      })

    },

    downLoadVersionFile(row) {

      var param = {

        token: getToken(),

        url: row.URL,

        versionNo: row.VersionNo

      }

      versionFileDownload(param).then(res => {

        if (res.code === 20000) {

          downloadFile(res.data.name, res.data.dataStr)

        }

      })

    }

  }

}

</script>



<style></style>

 其中,Dialog控制显隐的关键部分是watch函数部分

visible(val, oldVal) {

  if (val === oldVal) {

    return

  }

  this.innerVisible = val

},

innerVisible(val, oldVal) {

  if (val === oldVal) {

    return

  }

  this.$emit('update:visible', val)

}

 visible和innerVisible分别对应控件的属性以及Data,防止多次修改属性问题。

sURL(val, oldVal) {

      if (val === oldVal) {

        return

      }

      this.activeName = 'fileList'

      // 获取数据

      this.getList()

    }

  该部分是控制Dialog显示数据用

 

父画面调用:

<template>

  <div class="app-container">

    <div class="filter-container">

      <el-button v-waves size="small" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>

    </div>

    <el-table

      v-loading="listLoading"

      size="small"

      :data="list"

      style="width: 100%"

      element-loading-text="Loading"

      border

      stripe

      fit

      highlight-current-row

    >

      <el-table-column label="技术协议" align="center">

        <template slot-scope="scope">

          <el-button size="mini" @click="viewFile(scope.row)">查看</el-button>

        </template>

      </el-table-column>

    </el-table>

    <Pagination v-show="total > 0" size="small" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />



    <FileListDialog :url="fileURL" :visible.sync="fileListDialogVisible" />

  </div>

</template>



<script>

import FileListDialog from './dialog/fileListDialog.vue'



export default {

  components: {

    FileListDialog

  },

  data() {

    return {

      fileURL: '',

      fileListDialogVisible: false

    }

  },

  methods: {

    viewFile(row, type) {

      if (stringIsNullOrEmpty(row.Path)) return

      this.fileListDialogVisible = true

      this.fileURL = row.Path // URL地址

  }

 }

}

</script>

尊重作者劳动成果:https://www.cnblogs.com/byron-123/p/12610830.html

完 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值