要将Angular Material SVG图标设置为所有实体显示相同的图标,您可以按照以下步骤进行操作:
import { MatIconModule } from '@angular/material/icon';
import { MatIconRegistry } from '@angular/material/icon';
import { DomSanitizer } from '@angular/platform-browser';
@NgModule({
imports: [
// other imports
MatIconModule
],
// other declarations and providers
})
export class AppModule {
constructor(private matIconRegistry: MatIconRegistry, private domSanitizer: DomSanitizer) {
// register the SVG icon
this.matIconRegistry.addSvgIcon(
'custom-icon',
this.domSanitizer.bypassSecurityTrustResourceUrl('path/to/custom-icon.svg')
);
}
}
通过这种方式,您可以将指定的SVG图标应用于所有实体,并在不同地方重复使用相同的图标。您只需在NgModule的构造函数中注册图标,并在需要的地方使用mat-icon元素即可。