首页 > 解决方案 > +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>

标签: angular

解决方案


只是我必须进行一些更改才能修复它-我以前的代码是

+this.activatedRoute.snapshot.paramMap.get('id')

我的更正码是-

+this.activatedRoute.snapshot.params['id']

此代码工作正常


推荐阅读