Angular 5:在'ng serve --prod`之后抛出的错误“类型'上不存在属性'id'”

时间:2018-05-14 21:29:35

标签: angular

我的错误仅在Dim tables = SplitTable(dt, 8) 之后抛出,但在ng serve --prod之后一切正常。

错误

ng serve

HTML

ERROR in component.html(5,24): : Property 'id' does not exist on type 'ProductModel'.

我的组件

<div *ngIf="product?.id">{{product.id}}</div>

问题

  • 我设置product: ProductModel; constructor( public bcProductService: BcProductService) { this.bcProductService.getProductById(id).subscribe(single => { return this.product = single.data; }); } 错了吗?为什么它适用于开发而不是this.product
  • --prod似乎有一层额外的错误 如何确保在开发模式下看到所有错误?它是否像在ng build --prod中构建一样简单?但是对于environment.ts vars呢?

1 个答案:

答案 0 :(得分:2)

请勿使用product?.id而是使用product && product.id ? true : false或创建方法productExists(),它会为if返回true / false。 - 这可能与AoT有关(提前 - 可能在生产编译中使用) - 我在this angular-framework中找到了这些信息。