javascript - 使用 Github SVG 创建自定义 mat-icon

标签 javascript angular typescript svg angular-material

尝试创建自定义 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 包含了答案中的修复,所以它现在可以工作了!

最佳答案

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

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

关于javascript - 使用 Github SVG 创建自定义 mat-icon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58123766/

相关文章:

javascript - 如何在jsp中的Javascript中访问 map 键,值(java.util.Map)?

html - 无法在 Ionic 中扣除是否有折扣的实际价格

javascript - 使用 TypeScript 的 Django Compressor

typescript - JavaScript 和 Typescript 中模块导入的区别?

javascript - Raphaeljs 库和智能手机

javascript - 从 HTML 读取数字,添加数字并返回 HTML

javascript - Unicode 字符在变量名中有效吗?

css - 父级应该如何为 Angular 的子组件设置样式?我想要一个不会被弃用的好解决方案

angular - 根据 Angular 下拉菜单启用或禁用 formarray

javascript - 如何使用 useReducer 更新嵌套数组?