angular - +this.activatedRoute.snapshot.paramMap.get('id') 显示错误
问题描述
loadProduct() {
this.shopService.getProduct(+this.activatedRoute.snapshot.paramMap.get('id')).subscribe(product => {
this.product = product;
}, error => {
console.log(error);
});
}
在上面的代码中,产品详细信息显示并且工作正常,但我在第 2 行的 vs 代码中出现错误
+this.activatedRoute.snapshot.paramMap.get('id')
现在我能做什么?
错误:src/app/shop/product-details/product-details.component.ts:21:34 - 错误 TS2531:对象可能为“空”。21 this.shopService.getProduct(+this.activatedRoute.snapshot.paramMap.get('id')).subscribe(product => { ~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~</p>
解决方案
只是我必须进行一些更改才能修复它-我以前的代码是
+this.activatedRoute.snapshot.paramMap.get('id')
我的更正码是-
+this.activatedRoute.snapshot.params['id']
此代码工作正常
推荐阅读
- html - CSS - 选择标签的子标签,直到下一个相同类型的标签
- c# - C# Windows 窗体:不支持 SqlConnection 关键字,连接超时
- actions-on-google - “如何在谷歌助手操作中通过用户 ID 获取用户名?”
- css - 无法在css中居中图像
- python - 关于Python递归函数课程的问题
- json - 谷歌 json 风格指南:如何发送单项响应?
- javascript - 触发点击事件需要多长时间?
- python - 如何在使用 pandas 导入 csv 文件期间有效地删除重叠行?
- spring - RestController 无法正常工作
- c# - 如何处理 AMP 页面中已删除的推文