perf: 【IoT 物联网】场景联动 nextTick 优化使用 await nextTick()

This commit is contained in:
puhui999
2025-08-07 17:49:20 +08:00
parent 5b38c9c394
commit f3d1989832
4 changed files with 21 additions and 30 deletions

View File

@ -237,10 +237,6 @@ const handleSubmit = async () => {
// 提交请求 // 提交请求
submitLoading.value = true submitLoading.value = true
try { try {
// 数据结构已对齐,直接使用表单数据
console.log('提交数据:', formData.value)
// 调用API保存数据
if (isEdit.value) { if (isEdit.value) {
// 更新场景联动规则 // 更新场景联动规则
await RuleSceneApi.updateRuleScene(formData.value) await RuleSceneApi.updateRuleScene(formData.value)
@ -299,13 +295,12 @@ const initFormData = () => {
} }
// 监听抽屉显示 // 监听抽屉显示
watch(drawerVisible, (visible) => { watch(drawerVisible, async (visible) => {
if (visible) { if (visible) {
initFormData() initFormData()
// 重置表单验证状态 // 重置表单验证状态
nextTick(() => { await nextTick()
formRef.value?.clearValidate() formRef.value?.clearValidate()
})
} }
}) })

View File

@ -200,7 +200,7 @@ const handleTriggerTypeChange = (type: number) => {
} }
// 条件组相关方法 // 条件组相关方法
const addSubGroup = () => { const addSubGroup = async () => {
if (!trigger.value.conditionGroups) { if (!trigger.value.conditionGroups) {
trigger.value.conditionGroups = [] trigger.value.conditionGroups = []
} }
@ -211,11 +211,10 @@ const addSubGroup = () => {
} }
// 使用 nextTick 确保响应式更新完成后再添加新的子组 // 使用 nextTick 确保响应式更新完成后再添加新的子组
nextTick(() => { await nextTick()
if (trigger.value.conditionGroups) { if (trigger.value.conditionGroups) {
trigger.value.conditionGroups.push([]) trigger.value.conditionGroups.push([])
} }
})
} }
const removeSubGroup = (index: number) => { const removeSubGroup = (index: number) => {

View File

@ -107,7 +107,7 @@ const subGroup = useVModel(props, 'modelValue', emit)
const maxConditions = computed(() => props.maxConditions || 3) const maxConditions = computed(() => props.maxConditions || 3)
// 事件处理 // 事件处理
const addCondition = () => { const addCondition = async () => {
// 确保 subGroup.value 是一个数组 // 确保 subGroup.value 是一个数组
if (!subGroup.value) { if (!subGroup.value) {
subGroup.value = [] subGroup.value = []
@ -128,11 +128,10 @@ const addCondition = () => {
} }
// 使用 nextTick 确保响应式更新完成后再添加新条件 // 使用 nextTick 确保响应式更新完成后再添加新条件
nextTick(() => { await nextTick()
if (subGroup.value) { if (subGroup.value) {
subGroup.value.push(newCondition) subGroup.value.push(newCondition)
} }
})
} }
const removeCondition = (index: number) => { const removeCondition = (index: number) => {

View File

@ -462,25 +462,23 @@ const handleDataDisplay = (value: string) => {
// 监听外部值变化(编辑模式数据回显) // 监听外部值变化(编辑模式数据回显)
watch( watch(
() => localValue.value, () => localValue.value,
(newValue, oldValue) => { async (newValue, oldValue) => {
// 避免循环更新 // 避免循环更新
if (newValue === oldValue) return if (newValue === oldValue) return
// 使用 nextTick 确保在下一个 tick 中处理数据 // 使用 nextTick 确保在下一个 tick 中处理数据
nextTick(() => { await nextTick()
handleDataDisplay(newValue || '') handleDataDisplay(newValue || '')
})
}, },
{ immediate: true } { immediate: true }
) )
// 组件挂载后也尝试处理一次数据回显 // 组件挂载后也尝试处理一次数据回显
onMounted(() => { onMounted(async () => {
nextTick(() => { await nextTick()
if (localValue.value) { if (localValue.value) {
handleDataDisplay(localValue.value) handleDataDisplay(localValue.value)
} }
})
}) })
// 监听配置变化 // 监听配置变化