明树Git Lab

Commit 05d4eaef authored by zhanghan's avatar zhanghan

1

parent 92ea2af0
Pipeline #108418 passed with stage
in 20 seconds
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
<!-- 文件列表表格 --> <!-- 文件列表表格 -->
<el-table <el-table
v-if="!isInline" v-if="!isInline"
:data="fileList" :data="safeFileList"
style="width: 100%" style="width: 100%"
empty-text="暂无数据" empty-text="暂无数据"
border border
...@@ -66,21 +66,21 @@ ...@@ -66,21 +66,21 @@
placement="bottom-start" placement="bottom-start"
width="420" width="420"
trigger="hover" trigger="hover"
:disabled="fileList.length === 0 || disabled" :disabled="safeFileList.length === 0 || disabled"
popper-class="file-popover" popper-class="file-popover"
> >
<!-- 弹窗内容 --> <!-- 弹窗内容 -->
<template #default> <template #default>
<div class="file-popover-content"> <div class="file-popover-content">
<!-- 无文件提示 --> <!-- 无文件提示 -->
<div v-if="fileList.length === 0" class="empty-file-tip"> <div v-if="safeFileList.length === 0" class="empty-file-tip">
暂无上传文件 暂无上传文件
</div> </div>
<!-- 文件列表 --> <!-- 文件列表 -->
<div v-else class="file-list"> <div v-else class="file-list">
<div <div
v-for="(file, index) in fileList" v-for="(file, index) in safeFileList"
:key="file.id || index" :key="file.id || index"
class="file-item" class="file-item"
> >
...@@ -127,7 +127,7 @@ ...@@ -127,7 +127,7 @@
<template #reference> <template #reference>
<div class="inline-file"> <div class="inline-file">
<div class="file-count"> <div class="file-count">
<span class="count-num">{{ fileList.length }}</span> <span class="count-num">{{ safeFileList.length }}</span>
<span class="count-text">个文件</span> <span class="count-text">个文件</span>
</div> </div>
<el-icon class="upload-icon"><ArrowDown /></el-icon> <el-icon class="upload-icon"><ArrowDown /></el-icon>
...@@ -144,12 +144,12 @@ import { ElMessageBox, ElMessage } from "element-plus"; ...@@ -144,12 +144,12 @@ import { ElMessageBox, ElMessage } from "element-plus";
import moment from "moment"; import moment from "moment";
import windowConfig from "@/window"; import windowConfig from "@/window";
// 引入需要的图标 // 引入需要的图标
import { Document, Download, Delete } from "@element-plus/icons-vue"; import { Document, Download, Delete, ArrowDown } from "@element-plus/icons-vue"; // 补充缺失的 ArrowDown 图标
// 定义组件 props(设置默认值,无需外部传参 // 定义组件 props(放宽类型限制,增加兼容性
const props = defineProps({ const props = defineProps({
modelValue: { modelValue: {
type: Array, type: [Array, String, Number], // 兼容更多类型
default: () => [], default: () => [],
required: true, required: true,
}, },
...@@ -192,13 +192,15 @@ const emit = defineEmits(["update:modelValue"]); ...@@ -192,13 +192,15 @@ const emit = defineEmits(["update:modelValue"]);
const selectedIds = ref([]); const selectedIds = ref([]);
const popoverVisible = ref(false); const popoverVisible = ref(false);
// 计算属性:获取文件列表(双向绑定) // 核心:安全处理文件列表,将非数组值转为空数组
const fileList = computed({ const safeFileList = computed({
get() { get() {
return props.modelValue; // 校验:如果是数组则直接返回,否则转为空数组
return Array.isArray(props.modelValue) ? props.modelValue : [];
}, },
set(value) { set(value) {
emit("update:modelValue", value); // 向外发射的值始终是数组,保证数据类型统一
emit("update:modelValue", Array.isArray(value) ? value : []);
}, },
}); });
...@@ -210,7 +212,7 @@ const formatDate = (date) => { ...@@ -210,7 +212,7 @@ const formatDate = (date) => {
// 文件上传成功处理 // 文件上传成功处理
const handleUploadSuccess = (res) => { const handleUploadSuccess = (res) => {
if (res && res.data) { if (res && res.data) {
fileList.value = [...fileList.value, res.data]; safeFileList.value = [...safeFileList.value, res.data]; // 改用 safeFileList
console.log("上传成功:", res.data); console.log("上传成功:", res.data);
ElMessage.success("文件上传成功"); ElMessage.success("文件上传成功");
popoverVisible.value = false; popoverVisible.value = false;
...@@ -253,9 +255,9 @@ const handleDelete = (index) => { ...@@ -253,9 +255,9 @@ const handleDelete = (index) => {
type: "warning", type: "warning",
}) })
.then(() => { .then(() => {
const newList = [...fileList.value]; const newList = [...safeFileList.value]; // 改用 safeFileList
newList.splice(index, 1); newList.splice(index, 1);
fileList.value = newList; safeFileList.value = newList; // 改用 safeFileList
// 删除后关闭popover // 删除后关闭popover
popoverVisible.value = false; popoverVisible.value = false;
}) })
...@@ -270,20 +272,21 @@ const handleMultiDelete = () => { ...@@ -270,20 +272,21 @@ const handleMultiDelete = () => {
type: "warning", type: "warning",
}) })
.then(() => { .then(() => {
const newList = fileList.value.filter( const newList = safeFileList.value.filter(
// 改用 safeFileList
(item) => !selectedIds.value.includes(item.id), (item) => !selectedIds.value.includes(item.id),
); );
fileList.value = newList; safeFileList.value = newList; // 改用 safeFileList
selectedIds.value = []; selectedIds.value = [];
}) })
.catch(() => {}); .catch(() => {});
}; };
// 监听外部文件列表变化 // 监听外部文件列表变化(改为监听 safeFileList,无需额外处理)
watch( watch(
() => props.modelValue, () => safeFileList.value,
(newVal) => { (newVal) => {
fileList.value = newVal; // 空监听,仅保证响应式(safeFileList 已处理类型兼容)
}, },
{ deep: true }, { deep: true },
); );
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment