angular - 如何让 matIconRegistry.addSvgIcon 找到正确的路径

标签 angular svg angular-material icons

我使用自定义 svg 作为 mat-icon 的图标,但无法让它正确检索图标。如果我将它放在应用程序文件夹( Assets )旁边的文件夹中,它就可以工作,但它不适用于其他位置。我希望它专门针对与 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);
  }
}

最佳答案

您可以使用以下路径来完成:

.addSvgIcon('base4', this.setPath('assets/icons/bp4.svg')); 

关于angular - 如何让 matIconRegistry.addSvgIcon 找到正确的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68813853/

相关文章:

javascript - javascript中字符串中每个特殊字符前添加一个字符

Angular 6 和 Electron : Using a mocked class for unit testing and extend the real class does not work

text - svg连接不同样式的文本

javascript - SVG无法容纳浏览器窗口,窗口底部的白条

angular - 在 mat-select Angular 中设置文本

angular - 带有多个选项的 mat-select 中的样式复选框

javascript - Angular 8,用户输入并随用户输入和选定的复选标记一起更新

Angular4 : select [selected] not work for the first time

html - 带有两个动画的单个 SVG,一个仅通过 CSS 在另一个之后延迟播放?

css - AngularJS 中 mdDialog 的位置错误