如何阻止ABP框架中特定abp.notify.warn重复弹窗?
问题:ABP Framework中
preventDuplicates: true无法阻止重复警告弹窗 我正尝试阻止特定警告消息重复弹出(而非所有弹窗),目前出现了重复警告弹窗的问题。使用ABP Framework(基于Toastr库)时,timeOut: 1000参数生效正常,但preventDuplicates: true无法阻止重复弹窗,请问我遗漏了什么?哪里操作有误?
相关代码
CageAuto.ts
getMeasure(): IMeasurement { [...] if (heightPos <= 1) { throw new Error("Posterior Height is less than or equal to 1mm"); } [...] }
toolBase.ts
updateMeasures(): void { try { this.measurement = this.getMeasure(); } catch (e) { abp.notify.warn(e, '', { preventDuplicates: true, timeOut: 1000 }); return; } }
问题原因与解决办法
核心问题:消息内容识别失败
Toastr的preventDuplicates是基于消息文本内容做去重判断的,但你现在直接把Error对象传给了abp.notify.warn。每次抛出的Error都是新实例,即使消息文本相同,Toastr也会判定为不同的内容,自然无法阻止重复弹窗。修复方案
捕获错误时,提取Error对象的message属性作为警告文本传入:
updateMeasures(): void { try { this.measurement = this.getMeasure(); } catch (e) { // 改用e.message传递稳定的字符串消息 abp.notify.warn(e.message, '', { preventDuplicates: true, timeOut: 1000 }); return; } }
这样每次触发警告时传递的都是相同的字符串,preventDuplicates: true就能正常识别重复消息并阻止弹窗重复出现。
内容的提问来源于stack exchange,提问作者Arnaud VDR
相关产品推荐
相关产品推荐

