【发布时间】:2022-02-12 02:01:17
【问题描述】:
当我写这行html代码时:
<td> <img align="center" [src]="productByBarCode.imageUrl" /> </td>
控制台抛出如下错误:
ERROR TypeError: Cannot read properties of undefined (reading 'imageUrl')
imageUrl 是字符串类型,它是一个 url。我正在使用 Angular 材料。我该如何处理?
.html 文件:
<table>
<!-- Image Column -->
<ng-container matColumnDef="img">
<th mat-header-cell *matHeaderCellDef></th>
<td> <img align="center" [src]="productByBarCode.imageUrl" /> </td>
</ng-container>
...
</table>
.ts 文件:
export class ProductDetailsComponent implements OnInit {
public productByBarCode!: Product;
displayedColumns = ['img', 'name', 'description', 'price', 'quantity', 'add to cart'];
constructor(private _snackBar: MatSnackBar, private shared: SharedService, private productService: ProductService) { }
ngOnInit(): void {
this.shared.castByBarCode.subscribe(productByBarCode=>this.productByBarCode=productByBarCode);
...
}
【问题讨论】:
标签: html angular typescript angular-material