类已经或正在使用名称'SafeUrl'从外部模块获取,但不能命名

Class has or is using name 'SafeUrl' from external module but cannot be named

本文关键字:SafeUrl 从外部 模块 但不能 获取      更新时间:2023-09-26

我使用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);
  }
}

净化函数接受如下URL:

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

如果我使用AoT编译,我得到这个错误消息:

模块构建失败:错误:/…/src/app/app.component.ts (18,3):导出类的公共方法的返回类型具有或正在使用名称'SafeUrl'来自外部模块"/…/node_modules/@angular/platform-browser/src/安全/dom_sanitization_service"但不能命名)

我在Angular 2.1.0中使用CLI

有谁知道我怎样才能避开这个问题吗?还是应该作为bug报告?

所以似乎我必须添加一个返回类型的SafeUrl的方法

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

感谢alxhub

在我的例子中,我是这样初始化一个属性的:

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);