进行 DomSanitizer API 调用时不使用基本属性?

Not using the base attribute when making DomSanitizer API calls?

我正在像这样使用 DomSanitizer 进行调用:

        this.domSanitizer.bypassSecurityTrustResourceUrl(url));

url 已完成 URL 正在获取的 SVG 图像。

这是一个例子:

      this.matIconRegistry.addSvgIcon(
        "fs",
        this.domSanitizer.bypassSecurityTrustResourceUrl('https://github.com/fireflysemantics/images/blob/master/fsalpha/logo/fsalpha-logo-optimized.svg'));

然而,由于 baseindex.html 中设置为另一个 URL,dom 消毒剂 URL 以它为前缀。

有没有办法关闭它?

这是我写的一篇文章,展示了在没有添加 base 属性的情况下它是如何工作的:

https://medium.com/@ole.ersoy/angular-material-icon-from-github-hosted-svg-logo-d5c35b923d

这是一个 stackblitz,显示添加基本属性时,对绝对 URL 的调用以基本属性为前缀,因此传递的 URL 被破坏:

https://stackblitz.com/edit/angular-material-custom-icon-with-base?file=src/index.html

功能请求

Angular 对此问题有一个功能请求 WRT:

https://github.com/angular/angular/issues/23139

Angular 问题

https://github.com/angular/angular/issues/34645

错误截图

这是堆栈跟踪的屏幕截图,显示基础 URL 在图像 URL:

之前添加前缀

这看起来像是一个错误。 HttpClient 不可能重写具有协议的 URL。

Material 图标注册表不会改变图标的​​任何 URL。它实际上并没有对 URL 做任何特别的事情,只是推迟加载到 HttpClient。

https://github.com/angular/components/blob/09dc4594259cf1d6b34a5a0893c0bccaa132c319/src/material/icon/icon-registry.ts#L595

我无法重现这个,因为它需要将应用程序部署到 gh 页面之类的域,但是如果您看到这种情况发生,请在 Angular GitHub 项目并让他们知道。

我能给你的唯一答案是编写自定义拦截器来修复 URL。

这里有一篇博客解释如何使用拦截器来使用一个基URL。它不是关于如何解决您的问题的指南,但它是我能找到的最接近的东西。

http://www.projectcodify.com/angular-set-base-url-dynamically