我出错了
类型“ ShipProductDetail”上不存在属性“ pricePerItem”。
(产品类型为ProductDetail | ShipProductDetail)
但是我使用了某种类型的类型保护
<span
*ngIf="product.hasOwnProperty('pricePerItem')"
class="text-uppercase smaller"
>
{{ product.pricePerItem.priceWithVat | i18nNumber | ogCurrency }}
</span>
是否有错误?
这不是我的错:)
我和
一起解决了hasPricePerItem(
product: ProductDetail | ShipProductDetail
): PriceAndVatModel | null {
if ((product as ProductDetail).pricePerItem) {
return (product as ProductDetail).pricePerItem;
}
return null;
}
<span *ngIf="hasPricePerItem(product)" class="text-uppercase smaller">
{{ hasPricePerItem(product)?.priceWithVat | i18nNumber | ogCurrency }}
</span>
答案 0 :(得分:1)
不幸的是,typeguard无法与ngIf
一起使用。而且hasOwnProperty
不是类型保护者