【问题标题】:Creating an custom mat-icon with Github SVG使用 Github SVG 创建自定义 mat-icon
【发布时间】:2020-01-27 03:26:54
【问题描述】:

尝试创建自定义 SVG mat-icon 直接从 Github 加载 SVG。我首先尝试使用DomSanitizerdocumented the result in this article。所以 SVG 确实通过 HttpClient 加载。

现在我正在尝试直接通过DomSanitizer 这样做:

  constructor (private matIconRegistry: MatIconRegistry,
    private domSanitizer: DomSanitizer) {
          this.matIconRegistry.addSvgIcon(
      "logo",
      this.domSanitizer.bypassSecurityTrustResourceUrl("https://raw.githubusercontent.com/fireflysemantics/logo/master/l1.svg"));
    }

并在app.component.html 中进行渲染测试:

<mat-icon>logo</mat-icon>

而且没有 SVG 出现。我假设这条线:

this.domSanitizer.bypassSecurityTrustResourceUrl("https://raw.githubusercontent.com/fireflysemantics/logo/master/l1.svg"));

应该加载 svg,但我没有在网络选项卡中看到请求。另外,如果我们只是将原始 URL 粘贴到浏览器地址栏中:

https://raw.githubusercontent.com/fireflysemantics/logo/master/l1.svg

请求确实在网络标签中显示为l1.svg

想法?

Stackblitz

更新

上面的 Stackblitz 包含答案中的修复,所以它现在可以工作了!

【问题讨论】:

    标签: javascript angular typescript svg angular-material


    【解决方案1】:

    app.component.html 中的条目必须如下所示:

    <mat-icon svgIcon="logo"></mat-icon>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      • 2019-08-07
      • 1970-01-01
      • 2021-11-19
      • 2020-04-18
      • 1970-01-01
      • 2020-02-29
      相关资源
      最近更新 更多