新手学 Angular 人麻了. 附上代码
AComponent ts:
this.iconsLoaded$.subscribe((data: boolean) => {
this.icOnsLoaded= data;
this.cdr.detectChanges();
});
async registerIcons() {
for (const item of this.gridColumns) {
// 从 IndexedDB 中取出 svg
const svg: string | undefined = await getSvg(item);
if (!svg) {
const svgCOntent= await fetch(`icon/${item}.svg`).then((res) =>
res.text(),
);
await setSvg(item, svgContent); // 缓存到 IndexedDB
this.matIconRegistry.addSvgIconLiteral(
item,
this.sanitizer.bypassSecurityTrustHtml(svgContent),
);
} else {
this.matIconRegistry.addSvgIconLiteral(
item,
this.sanitizer.bypassSecurityTrustHtml(svg),
);
}
}
return true;
}
BComponent ts:
await this.AComponent.registerIcons();
this.AComponent.iconSubject.next(true);
A html:
<ng-container class="grid-container">
<mat-grid-list cols="2" rowHeight="3:1">
@
if (iconsLoaded$ | async) {
@
for (item of gridColumns; track item) {
<mat-grid-tile class="grid-item">
<div class="content-container">
<mat-icon [svgIcon]="item"></mat-icon>
<span>
{{ cityInfo[item] }}
</span>
</div>
</mat-grid-tile>
}
}
</mat-grid-list>
</ng-container>