angular - Angular 8:对对象执行乘法时出现编译错误
问题描述
我在编译我的角度代码时遇到问题
ERROR in src/app/order-details/order-details.component.html:76:55 - error TS2531: Object is possibly 'null'.
76 <div class="mat-display-1">you have to pay {{(productNumber | async ) * 90 }} $</div>
但是我在 UI 上得到了正确的值。
我可以进行任何检查以消除此错误。
更多代码
constructor(private http : HttpClientService) {
this.productNumber=this.http.orderDetailEmitter;
}
解决方案
您遇到的是竞争条件。您在 orderDetailEmitter observable 具有有效的整数值之前订阅它。这个是正常的。
你有两个我知道的修复。
- 合并值:
{{ ( (productNumber | async ) || 0 )* 90 }}
- 使用 ngIf
<div *ngIf="productNumber | async" class="mat-display-1">you have to pay {{(productNumber | async ) * 90 }} $</div>
其中任何一个都可以解决问题。
推荐阅读
- python - SQLAlchemy - 何时创建额外的模型和关系与仅将 JSON 存储在列中?
- php - 更新 Select 查询循环所基于的字段
- amazon-web-services - AWS MQTT 代理/规则调用中的日志信息不足
- c - C中的recv函数无法获得价值
- spring-boot - 使用动态主机和端口的 TCP 套接字客户端的 Spring 集成
- java - StreamResource 有时在关闭对话框窗口中单击下载按钮时找不到资源
- javascript - 为什么在使用 for 循环添加事件侦听器时会出现 TypeError?
- javascript - 当我使用 for 或 while 循环时,我的应用程序崩溃了
- c++ - glGenVertexArrays 上的 OpenGL C++ glfw 3 glew 错误 1282
- javascript - 在 IE 8 中显示图像,image/abc.gif 和 image\abc.gif 都可以正常工作,但在 IE 11 中却不是。有人可以帮我解决这个问题吗