简体   繁体   English

角-“ changingThisBreaksApplicationSecurity”给出了ts错误“类型'SafeUrl'上不存在属性'changingThisBreaksApplicationSecurity'。”

[英]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.

相关问题 角度6:错误TS2339:类型“ HTMLElement”上不存在属性“值” - Angular 6: error TS2339: Property 'value' does not exist on type 'HTMLElement' Angular 管道上的 .includes 返回“属性 &#39;includes&#39; 在类型 &#39;never&#39;.ts(2339)&#39; 上不存在错误消息 - .includes on Angular pipe is returning 'Property 'includes' does not exist on type 'never'.ts(2339)' error message 如何解决 Angular 订阅错误? 类型“AngularFireList”上不存在属性“订阅”<unknown> &#39;.ts(2339) - How to solve Angular subscribe error? Property 'subscribe' does not exist on type 'AngularFireList<unknown>'.ts(2339) Angular2打字稿错误TS2339:类型“ Y”上不存在属性“ x” - Angular2 typescript error TS2339: Property 'x' does not exist on type 'Y' Angular 错误 TS2339:“车辆 []”类型上不存在属性“车辆” - Angular Error TS2339: Property 'vehicle' does not exist on type 'Vehicle[]' 错误TS2339:类型“字符串”上不存在属性“默认”。** - Error TS2339: Property 'Default' does not exist on type 'string'.** 错误TS2339:类型“ HTMLElement”上不存在属性“ imageID” - error TS2339: Property 'imageID' does not exist on type 'HTMLElement' 错误TS:2339类型{}上没有属性“ Project” - error TS: 2339 Property “Project” does not exist on type { } Typescript 错误:属性“status”在类型“never”上不存在。ts(2339) - Typescript error: Property 'status' does not exist on type 'never'.ts(2339) 使用 Redux 调度操作出现 TS 错误,“类型上不存在属性” - Getting TS error with Redux dispatch action, `Property does not exist on type`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM