js陷阱

位置:首页 / 技术中心 / 杂项

杂项 Admin 2026-06-02 21:03:12 186

21:00

小黄学长


getDishServiceDetail,接口返回的"dishType": 0, dishManagement.vue 302-305 没有回显出来


前端电脑端架构师


思考过程


让我先查看这个文件的完整内容,了解 viewForm 是如何被赋值的。


src\views\institutions\elderlyInstitution\toDoService\components\dishManagement.vue


Thought


我找到问题了!在第 666 行左右的 handleClick 方法中,当处理"查看"操作时:


```

this.viewForm.dishType = detail.

dishType || null;

```

问题原因 :当 detail.dishType 是 0 时,由于 0 在 JavaScript 中是 falsy 值, detail.dishType || null 会返回 null ,导致餐次无法正确回显。


让我修复这个问题:


dishManagement.vue

src\views\institutions\elderlyInstitution\toDoService\components\dishManagement.vue


+ 1 - 1


查看变更


Thought


已修复!问题出在 handleClick 方法中处理"查看"操作时的赋值逻辑。


问题分析 :


- 原代码: this.viewForm.dishType = detail.dishType || null;

- 当接口返回 dishType: 0 (早餐)时,由于 0 在 JavaScript 中是 falsy 值 , || 运算符会返回右侧的 null

- 导致模板中的判断 viewForm.dishType === 0 永远不成立

修复方案 :


- 使用空值合并运算符 ?? 替代 ||

- ?? 只在值为 null 或 undefined 时才返回右侧值, 0 会被正确保留

现在当 dishType 为 0 时,页面会正确显示"早餐"。

18455111359 扫描二维码