【问题标题】:Class has or is using name 'SafeUrl' from external module but cannot be named类具有或正在使用来自外部模块的名称“SafeUrl”,但无法命名
【发布时间】:2018-02-03 00:00:13
【问题描述】:

我正在使用sanitizer.bypassSecurityTrustUrl 在页面上放置指向 blobURL 的链接。只要我不 AoT 编译项目,这工作就很好。

import {DomSanitizer} from '@angular/platform-browser';

export class AppComponent {
  constructor(private sanitizer: DomSanitizer) {
  }

  sanitize(url: string) {
    return this.sanitizer.bypassSecurityTrustUrl(url);
  }
}

sanitize 函数采用如下 URL:

blob:http://localhost:4200/7c1d7221-aa0e-4d98-803d-b9be6400865b

如果我使用 AoT 编译,我会收到以下错误消息:

模块构建失败:错误:/.../src/app/app.component.ts (18,3): 导出类的公共方法的返回类型已经或正在使用名称 来自外部模块的“SafeUrl” “/.../node_modules/@angular/platform-b​​rowser/src/security/dom_sanitization_service” 但不能命名。)

我在 Angular 2.1.0 中使用 CLI

任何人都知道我可以如何规避这个问题?还是应该将其报告为错误?

【问题讨论】:

    标签: javascript angular angular2-cli angular2-aot


    【解决方案1】:

    所以看来我必须在方法中添加SafeUrl 的返回类型

      sanitize(url: string):SafeUrl {
        return this.sanitizer.bypassSecurityTrustUrl(url);
      }
    

    非常感谢alxhub

    【讨论】:

      【解决方案2】:

      在我的情况下,我正在启动这样的属性:

      public img64 = this.domSanitizer.bypassSecurityTrustResourceUrl('data:image/jpg;base64,' + this.base64Image);
      

      导致同样的错误。

      感谢@mottosson,我做对了(只需添加 SafeUrl 类型):

      public img64: SafeUrl = this.domSanitizer.bypassSecurityTrustResourceUrl('data:image/jpg;base64,' + this.base64Image);
      

      【讨论】:

        猜你喜欢
        • 2017-10-09
        • 2018-01-06
        • 2019-06-06
        • 1970-01-01
        • 2017-09-06
        • 2020-04-05
        • 2018-07-09
        • 1970-01-01
        • 2019-03-29
        相关资源
        最近更新 更多