简体   繁体   English

类具有或正在使用外部模块的名称“ SafeUrl”,但无法命名

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

I'm using sanitizer.bypassSecurityTrustUrl to put links to blobURL's on the page. 我正在使用sanitizer.bypassSecurityTrustUrl在页面上放置指向blobURL的链接。 This works just fine as long as I don't AoT compile the project. 只要我不编译项目,此方法就可以正常工作。

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

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

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

The sanitize function takes a URL like this: 清理功能采用如下网址:

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

If I use AoT compilation I get this error message: 如果我使用AoT编译,则会收到以下错误消息:

Module build failed: Error: /.../src/app/app.component.ts (18,3): Return type of public method from exported class has or is using name 'SafeUrl' from external module "/.../node_modules/@angular/platform-browser/src/security/dom_sanitization_service" but cannot be named.) 模块构建失败:错误:/.../src/app/app.component.ts(18,3):导出类的公共方法的返回类型具有或正在使用外部模块“ / ...”中的名称“ SafeUrl” / node_modules / @ angular / platform-b​​rowser / src / security / dom_sanitization_service”,但无法命名。)

I'm using CLI with Angular 2.1.0 我在Angular 2.1.0中使用CLI

Anybody knows how I can circumvent this problem? 有人知道我该如何解决这个问题? Or should it be reported as a bug? 还是应该将其报告为错误?

So it seems I had to add a return type of SafeUrl to the method 因此,似乎我不得不向该方法添加SafeUrl的返回类型

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

Big thanks to alxhub 非常感谢alxhub

In my case i was initiating an attribute like this : 就我而言,我正在初始化这样的属性:

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

Resulting in the same error. 导致相同的错误。

Thanks to @mottosson I got it right (just add the type SafeUrl): 感谢@mottosson,我弄对了(只需添加类型SafeUrl):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular-CLI错误:导出类的公共方法的返回类型具有或正在使用外部模块的名称“ ErrorObservable”,但无法命名 - Angular-CLI Error: Return type of public method from exported class has or is using name 'ErrorObservable' from external module but cannot be named 找不到名称“ SafeUrl” - Cannot find name 'SafeUrl' requirejs-无法定义命名模块 - requirejs - Cannot define named module 无法模拟外部节点模块 - Cannot mock external node module 无法使用browserify从其他本地打字稿模块使用打字稿类 - Cannot use typescript class from other local typescript module using browserify 找不到名称“模块” - Cannot find name 'module' Angular2错误:导出类的公共方法的返回类型具有或正在使用私有名称 - Angular2 Error: Return type of public method from exported class has or is using private name 通过类名称从jQuery获取外部文件的div - Get divs from an external file by class name.with jQuery 无法使用一个套接字模块将变量名从一个节点模块发送到另一个节点模块,以将其用作聊天应用程序的房间名称 - Cannot send variable name from one node module to another to use it as a room-name for chat application using socket.io 嵌套:无法创建名为“默认”的新连接,因为具有该名称的连接已经存在并且它现在具有活动连接 session - Nest: Cannot create a new connection named "default", because connection with such name already exist and i t now has an active connection session
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM