<div v-else-if="per.dict_value === 'file_name'"> <el-tooltip placement="top" effect="dark" v-if="scope.row.download_time || scope.row.repair_download_time"> <div slot="content" > <span v-if="scope.row.repair_download_time">{{ parseTime(scope.row.repair_download_time,'{y}-{m}-{d} {h}:{i}:{s}') }}</span> <span v-else-if="scope.row.download_time">{{ parseTime(scope.row.download_time,'{y}-{m}-{d} {h}:{i}:{s}') }}</span> </div> <el-link v-if="scope.row.repair_file_id" :key="scope.row.repair_file_id" style="display: block;color: #67C23A" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.repair_file_id, scope.row.order_no, scope.row.repair_file_name)" >{{ scope.row.repair_file_name }}</el-link> <el-link v-else-if="scope.row.file_id" :key="scope.row.file_id" style="display: block;color: #67C23A" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.file_id, scope.row.order_no, scope.row.file_name)" >{{ scope.row[per.dict_value] }}</el-link> </el-tooltip> <div v-else> <el-link v-if="scope.row.repair_file_id" :key="scope.row.repair_file_id" style="display: block;" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.repair_file_id, scope.row.order_no, scope.row.repair_file_name)" >{{ scope.row.repair_file_name }}</el-link> <el-link v-else-if="scope.row.file_id" :key="scope.row.file_id" style="display: block;" icon="el-icon-paperclip" @click="downloadOrderExcel(scope.row.file_id, scope.row.order_no, scope.row.file_name)" >{{ scope.row[per.dict_value] }}</el-link> </div> </div>优化这段代码
时间: 2024-02-14 08:10:03 浏览: 74
python预科班资料.zip_Python 0-预科班_python预科班资料_silkhss
这段代码可以进行如下优化:
1. 将 el-tooltip 改为使用 v-show 替代 v-if,这样 el-tooltip 将一直存在于 DOM 中,只是通过 CSS 的 display 属性来控制显示和隐藏。这样可以避免每次渲染时都需要重新创建 el-tooltip。
2. 将 el-link 代码提取出来,避免重复代码,同时可以更方便的修改样式和事件绑定。
3. 将 parseTime 方法提取出来,避免在模板中重复调用。
优化后的代码如下:
```html
<div v-else-if="per.dict_value === 'file_name'">
<el-tooltip placement="top" effect="dark" :content="tooltipContent">
<template v-slot:content>
<span>{{ tooltipTime }}</span>
<el-link v-if="fileId" :key="fileId" class="link" icon="el-icon-paperclip" @click="downloadExcel">{{ fileName }}</el-link>
</template>
<el-link v-else-if="repairFileId" :key="repairFileId" class="link" style="color: #67C23A" icon="el-icon-paperclip" @click="downloadExcel">{{ repairFileName }}</el-link>
</el-tooltip>
</div>
```
```javascript
data() {
return {
tooltipTime: '',
fileId: '',
fileName: '',
repairFileId: '',
repairFileName: '',
}
},
computed: {
tooltipContent() {
return this.tooltipTime ? '<span>' + this.tooltipTime + '</span>' + this.$refs.link.$el.outerHTML : ''
},
},
methods: {
downloadExcel() {
// 下载文件的代码
},
parseTime(time, format) {
// 解析时间的代码
},
},
mounted() {
const { scope } = this
if (scope.row.repair_download_time) {
this.tooltipTime = this.parseTime(scope.row.repair_download_time, '{y}-{m}-{d} {h}:{i}:{s}')
this.repairFileId = scope.row.repair_file_id
this.repairFileName = scope.row.repair_file_name
} else if (scope.row.download_time) {
this.tooltipTime = this.parseTime(scope.row.download_time, '{y}-{m}-{d} {h}:{i}:{s}')
this.fileId = scope.row.file_id
this.fileName = scope.row.file_name
}
},
```
通过上述优化,可以避免每次渲染时都重新创建 el-tooltip 和 el-link,同时也可以使代码更加简洁易懂。
阅读全文