refactor(user): 移除不必要的组件状态追踪代码
- 删除 isComponentMounted 状态变量及相关检查逻辑 - 简化异步函数中的组件挂载状态验证 - 保留必要的资源清理逻辑(定时器和Blob URL) - 优化代码结构,提高可维护性 Vue 3 的响应式系统已经能够自动处理组件卸载时的状态更新, 无需手动追踪组件生命周期状态。
This commit is contained in:
parent
7254d8f9bd
commit
92ab0c3159
@ -67,8 +67,6 @@ const selectedFile = ref<File | null>(null)
|
||||
const updateSupport = ref(false)
|
||||
const uploadProgress = ref(0)
|
||||
|
||||
// 组件状态追踪
|
||||
const isComponentMounted = ref(true)
|
||||
const progressInterval = ref<NodeJS.Timeout | null>(null)
|
||||
const createdBlobUrls = ref<string[]>([])
|
||||
|
||||
@ -113,6 +111,7 @@ const rules = {
|
||||
{ type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' },
|
||||
],
|
||||
phone: [
|
||||
{ required: true, message: '请输入手机号', trigger: 'blur' },
|
||||
{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' },
|
||||
],
|
||||
password: [
|
||||
@ -351,9 +350,6 @@ const columns: DataTableColumns<UserVo> = [
|
||||
|
||||
// 获取用户列表
|
||||
async function getUserList() {
|
||||
if (!isComponentMounted.value)
|
||||
return
|
||||
|
||||
loading.value = true
|
||||
try {
|
||||
// 构建请求参数,处理时间范围
|
||||
@ -381,10 +377,6 @@ async function getUserList() {
|
||||
|
||||
const { isSuccess, data } = await fetchUserPage(params)
|
||||
|
||||
// 检查组件是否仍然挂载
|
||||
if (!isComponentMounted.value)
|
||||
return
|
||||
|
||||
if (isSuccess && data) {
|
||||
tableData.value = data.records || []
|
||||
pagination.value.itemCount = data.total || 0
|
||||
@ -397,18 +389,14 @@ async function getUserList() {
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
if (!isComponentMounted.value)
|
||||
return
|
||||
console.error('获取用户列表失败:', error)
|
||||
coiMsgError('获取用户列表失败,请检查网络连接')
|
||||
tableData.value = []
|
||||
pagination.value.itemCount = 0
|
||||
}
|
||||
finally {
|
||||
if (isComponentMounted.value) {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取角色列表
|
||||
@ -890,10 +878,6 @@ async function handleConfirmImport() {
|
||||
|
||||
const response = await importUserData(selectedFile.value, updateSupport.value)
|
||||
|
||||
// 检查组件是否仍然挂载
|
||||
if (!isComponentMounted.value)
|
||||
return
|
||||
|
||||
if (progressInterval.value) {
|
||||
clearInterval(progressInterval.value)
|
||||
progressInterval.value = null
|
||||
@ -915,11 +899,9 @@ async function handleConfirmImport() {
|
||||
}
|
||||
}
|
||||
catch (error) {
|
||||
if (isComponentMounted.value) {
|
||||
console.error('导入失败:', error)
|
||||
coiMsgError('导入失败,请检查文件格式或联系管理员')
|
||||
}
|
||||
}
|
||||
finally {
|
||||
// 清理定时器
|
||||
if (progressInterval.value) {
|
||||
@ -927,11 +909,9 @@ async function handleConfirmImport() {
|
||||
progressInterval.value = null
|
||||
}
|
||||
|
||||
if (isComponentMounted.value) {
|
||||
importLoading.value = false
|
||||
uploadProgress.value = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 取消导入
|
||||
@ -1105,9 +1085,6 @@ onMounted(() => {
|
||||
|
||||
// 组件卸载前清理资源
|
||||
onBeforeUnmount(() => {
|
||||
// 标记组件已卸载
|
||||
isComponentMounted.value = false
|
||||
|
||||
// 清理定时器
|
||||
if (progressInterval.value) {
|
||||
clearInterval(progressInterval.value)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user