typescript SafeValue 必须使用 [property]=binding,但它是

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47641492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:05:33  来源:igfitidea点击:

SafeValue must use [property]=binding, but IT IS

angulartypescript

提问by Fede E.

Hello all, this is driving me nuts. I tried all the fixes I was able to find online and nothing seems to work.

大家好,这让我发疯。我尝试了所有可以在网上找到的修复程序,但似乎没有任何效果。

I am loading the base64 image info from a DB, which returns the base64 encoded image, like this:

我正在从数据库加载 base64 图像信息,它返回 base64 编码的图像,如下所示:

/9j/4AAQSkZJRgABAQEBLAEsAAD/4UHuRXhpZgAATU0AKgAAAAgABgEaAAUAAAABAAAAVgEbAAUAAAABAAAAXgEoAAMAAAABAAIAAAITAAMAAAABAAEAAIdpAAQAAAABAAAIcuocAAcAAAgMAAAAZgAAEOQAAAEsAAAAAQAAASwAAAABHOoAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

Then in my TS file I have the following function:

然后在我的 TS 文件中,我有以下功能:

photo_url(data:string){

     this.user_photo = this.domSanitizer.bypassSecurityTrustResourceUrl('data:image/jpeg;base64,' + data).toString();

}

My HTML file looks as follows:

我的 HTML 文件如下所示:

<img [src]="user_photo ? user_photo : 'assets/img/default.png'">

However I am getting the following error and the image is not displaying:

但是我收到以下错误并且图像没有显示:

WARNING: sanitizing unsafe URL value SafeValue must use [property]=binding: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEBLAEsAAD/4UHuRXhpZgAATU0AKgAAAAgABgEaAAUAAAABAAAAVgEbAAUAAAABAAAAXgEoAAMAAAABAAIAAAITAAMAAAABAAEAAIdpAAQAAAABAAAIcuocAAcAAAgMAAAAZgAAEOQAAAEsAAAAAQAAASwAAAABHOoAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...

Error says sanitizer should uses property binding, and it is, but it is still showing that error and the image does not display.

错误说 sanitizer 应该使用属性绑定,确实如此,但它仍然显示该错误并且图像不显示。

Am I missing something here?

我在这里错过了什么吗?

Thanks!

谢谢!

采纳答案by Explosion Pills

When you use .toStringyou are setting user_phototo a string instead of a SafeResourceUrlas required.

当您使用时,.toString您将根据需要设置user_photo为字符串而不是 a SafeResourceUrl

Actually simply removing the .toString()will make this work properly. Otherwise you are essentially just undoing the work of the DomSanitizer.

实际上,只需删除.toString()将使此正常工作。否则,您实际上只是在撤消 DomSanitizer 的工作。

user_photo: SafeResourceUrl;
photo_url(data: string){
     this.user_photo = this.domSanitizer.bypassSecurityTrustResourceUrl(
       'data:image/jpeg;base64,' + data);
}