【发布时间】:2021-10-19 03:44:18
【问题描述】:
我使用自定义 svg 作为 mat-icon 的图标,但无法正确检索图标。如果我将它放在应用程序文件夹(资产)旁边的文件夹中,它可以工作,但它不适用于其他位置。我希望它专门用于与 icon.module 位于同一文件夹中的图标以及作为 src 兄弟的文件夹。我怎样才能做到这一点?我不知道为什么它不起作用..
这里是 icon.module.ts:
import { NgModule } from '@angular/core';
import { DomSanitizer, SafeResourceUrl } from '@angular/platform-browser';
import { MatIconRegistry, MatIconModule } from '@angular/material/icon';
@NgModule({
imports: [MatIconModule]})
export class IconModule {
private path = '../../script/icons';
private path2 = '../assets/icons';
private path3 = 'app/icons';
constructor(
private domSanitizer: DomSanitizer,
public matIconRegistry: MatIconRegistry ) {
this.matIconRegistry
.addSvgIcon('base1', this.setPath(`${this.path}/bp.svg`))
.addSvgIcon('base2', this.setPath(`${this.path2}/bp.svg`))
.addSvgIcon('base3', this.setPath('src/app/icons/bp3.svg'));
//.addSvgIcon('base', this.setPath('bp2.svg'));
}
private setPath(url: string): SafeResourceUrl {
return this.domSanitizer.bypassSecurityTrustResourceUrl(url);
}
}
【问题讨论】:
标签: angular svg angular-material icons