带ID参数的删除函数如何排除ID等于1的场景不执行?
解决方案
你只需要在删除逻辑执行前增加ID校验,拦截ID为1的请求即可,以下是不同场景的实现:
1. 无提示最简实现
不需要给用户任何反馈时直接拦截返回即可:
const handleDeleteEmail = (emailId: number) => { if (emailId === 1) return; dispatch(deleteEmailTemplate(emailId)); EmailTemplatesApi.deleteEmailTemplate(emailId); };
2. 带用户提示实现
需要告知用户该内容不可删除时,增加提示逻辑即可,示例中用alert做演示,你可以替换为项目内的通用提示组件(如Toast、Modal等):
const handleDeleteEmail = (emailId: number) => { if (emailId === 1) { alert('该默认模板不允许删除'); return; } dispatch(deleteEmailTemplate(emailId)); EmailTemplatesApi.deleteEmailTemplate(emailId); };
可扩展优化方案
如果后续可能增加更多禁止删除的ID,建议把禁止删除的ID抽为常量数组,后续修改只需调整数组内容即可:
// 禁止删除的模板ID列表,可按需扩展 const FORBIDDEN_DELETE_IDS = [1]; const handleDeleteEmail = (emailId: number) => { if (FORBIDDEN_DELETE_IDS.includes(emailId)) { alert('该模板不允许删除'); return; } dispatch(deleteEmailTemplate(emailId)); EmailTemplatesApi.deleteEmailTemplate(emailId); };
内容的提问来源于stack exchange,提问作者Serhii Kusnetsov
相关产品推荐
相关产品推荐

