Angular 5 - Stop errors from undefined object before loading data

后端 未结 4 1037
旧时难觅i
旧时难觅i 2021-02-15 13:57

What\'s the best way to prevent errors in console from objects that are still undefined?

Let\'s say I have this

name : string;
constructor(private data:          


        
4条回答
  •  爱一瞬间的悲伤
    2021-02-15 14:02

    You can use {{name?.value}} or {{name?}} inside component.html. Can make treatment to like this in compoment.ts ↓

       ````
    
        this.route.paramMap.pipe(
          map((param: ParamMap) => {
            // @ts-ignore
            return param.params.id;
          })
        ).subscribe(prodId => {
          this.id = prodId;
          this.productService.getSingleProduct(this.id).subscribe(prod => {
            this.product = prod;
            if (prod.images !== null) {
              this.thumbimages = prod.images.split(';');
            }`enter code here`
          });
        });
    
    
      ````
    

提交回复
热议问题