[英]angular -“changingThisBreaksApplicationSecurity” gives ts error “Property 'changingThisBreaksApplicationSecurity' does not exist on type 'SafeUrl'.”
In my app I am displaying an image in this way- 在我的应用中,我以这种方式显示图像-
loadProfileImage(userID){
this.settingsService.getProfileImage(userID).subscribe((data) =>{
if (data){
this.profileImage = data;
const imageBlobUrl = this.sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL( data ));
this.profileImage = imageBlobUrl.changingThisBreaksApplicationSecurity;
}
});
}
All works good, when I debug I can see that the param imageBlobUrl is from type SafeUrlImpl and does have the field "changingThisBreaksApplicationSecurity", but when I run production build I get this error "Property 'changingThisBreaksApplicationSecurity' does not exist on type 'SafeUrl'", how can I fix it? 一切正常,当我调试时,我可以看到参数imageBlobUrl来自SafeUrlImpl类型,并且确实具有字段“ changingThisBreaksApplicationSecurity”,但是当我运行生产版本时,出现此错误“ Property'changingThisBreaksApplicationSecurity'在类型'SafeUrl'上不存在”,我该如何解决?
You should not use that property, it's a private property. 您不应该使用该属性,它是私有属性。 You should just assign the
SafeUrl
to your profileImage
: 您应该将
SafeUrl
分配给您的profileImage
:
profileImage?: SafeUrl;
loadProfileImage(userID){
this.settingsService.getProfileImage(userID).subscribe((data) =>{
if (data){
this.profileImage = this.sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL( data ));
}
});
}
which you can use in your template like: 您可以在模板中使用它,例如:
<img *ngIf="profileImage" [src]="profileImage">
note: you can also make use of a custom SafeUrl pipe which does this for you. 注意:您也可以使用自定义的SafeUrl管道为您执行此操作。 In combination with the
async
pipe you make your code a lot smaller 结合使用
async
管道,您可以使代码更小
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.