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 updateSupport = ref(false)
|
||||||
const uploadProgress = ref(0)
|
const uploadProgress = ref(0)
|
||||||
|
|
||||||
// 组件状态追踪
|
|
||||||
const isComponentMounted = ref(true)
|
|
||||||
const progressInterval = ref<NodeJS.Timeout | null>(null)
|
const progressInterval = ref<NodeJS.Timeout | null>(null)
|
||||||
const createdBlobUrls = ref<string[]>([])
|
const createdBlobUrls = ref<string[]>([])
|
||||||
|
|
||||||
@ -113,6 +111,7 @@ const rules = {
|
|||||||
{ type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' },
|
{ type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' },
|
||||||
],
|
],
|
||||||
phone: [
|
phone: [
|
||||||
|
{ required: true, message: '请输入手机号', trigger: 'blur' },
|
||||||
{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' },
|
{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' },
|
||||||
],
|
],
|
||||||
password: [
|
password: [
|
||||||
@ -351,9 +350,6 @@ const columns: DataTableColumns<UserVo> = [
|
|||||||
|
|
||||||
// 获取用户列表
|
// 获取用户列表
|
||||||
async function getUserList() {
|
async function getUserList() {
|
||||||
if (!isComponentMounted.value)
|
|
||||||
return
|
|
||||||
|
|
||||||
loading.value = true
|
loading.value = true
|
||||||
try {
|
try {
|
||||||
// 构建请求参数,处理时间范围
|
// 构建请求参数,处理时间范围
|
||||||
@ -381,10 +377,6 @@ async function getUserList() {
|
|||||||
|
|
||||||
const { isSuccess, data } = await fetchUserPage(params)
|
const { isSuccess, data } = await fetchUserPage(params)
|
||||||
|
|
||||||
// 检查组件是否仍然挂载
|
|
||||||
if (!isComponentMounted.value)
|
|
||||||
return
|
|
||||||
|
|
||||||
if (isSuccess && data) {
|
if (isSuccess && data) {
|
||||||
tableData.value = data.records || []
|
tableData.value = data.records || []
|
||||||
pagination.value.itemCount = data.total || 0
|
pagination.value.itemCount = data.total || 0
|
||||||
@ -397,17 +389,13 @@ async function getUserList() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
if (!isComponentMounted.value)
|
|
||||||
return
|
|
||||||
console.error('获取用户列表失败:', error)
|
console.error('获取用户列表失败:', error)
|
||||||
coiMsgError('获取用户列表失败,请检查网络连接')
|
coiMsgError('获取用户列表失败,请检查网络连接')
|
||||||
tableData.value = []
|
tableData.value = []
|
||||||
pagination.value.itemCount = 0
|
pagination.value.itemCount = 0
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
if (isComponentMounted.value) {
|
loading.value = false
|
||||||
loading.value = false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -890,10 +878,6 @@ async function handleConfirmImport() {
|
|||||||
|
|
||||||
const response = await importUserData(selectedFile.value, updateSupport.value)
|
const response = await importUserData(selectedFile.value, updateSupport.value)
|
||||||
|
|
||||||
// 检查组件是否仍然挂载
|
|
||||||
if (!isComponentMounted.value)
|
|
||||||
return
|
|
||||||
|
|
||||||
if (progressInterval.value) {
|
if (progressInterval.value) {
|
||||||
clearInterval(progressInterval.value)
|
clearInterval(progressInterval.value)
|
||||||
progressInterval.value = null
|
progressInterval.value = null
|
||||||
@ -915,10 +899,8 @@ async function handleConfirmImport() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
if (isComponentMounted.value) {
|
console.error('导入失败:', error)
|
||||||
console.error('导入失败:', error)
|
coiMsgError('导入失败,请检查文件格式或联系管理员')
|
||||||
coiMsgError('导入失败,请检查文件格式或联系管理员')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
// 清理定时器
|
// 清理定时器
|
||||||
@ -927,10 +909,8 @@ async function handleConfirmImport() {
|
|||||||
progressInterval.value = null
|
progressInterval.value = null
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isComponentMounted.value) {
|
importLoading.value = false
|
||||||
importLoading.value = false
|
uploadProgress.value = 0
|
||||||
uploadProgress.value = 0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1105,9 +1085,6 @@ onMounted(() => {
|
|||||||
|
|
||||||
// 组件卸载前清理资源
|
// 组件卸载前清理资源
|
||||||
onBeforeUnmount(() => {
|
onBeforeUnmount(() => {
|
||||||
// 标记组件已卸载
|
|
||||||
isComponentMounted.value = false
|
|
||||||
|
|
||||||
// 清理定时器
|
// 清理定时器
|
||||||
if (progressInterval.value) {
|
if (progressInterval.value) {
|
||||||
clearInterval(progressInterval.value)
|
clearInterval(progressInterval.value)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user