首页 > 解决方案 > 角度 -“changeingThisBreaksApplicationSecurity”给出 ts 错误“'SafeUrl' 类型上不存在属性 'changedThisBreaksApplicationSecurity'。”

问题描述

在我的应用程序中,我以这种方式显示图像-

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

一切正常,当我调试时,我可以看到参数 imageBlobUrl 来自 SafeUrlImpl 类型并且确实具有字段“changedThisBreaksApplicationSecurity”,但是当我运行生产构建时,我收到此错误“Property 'changedThisBreaksApplicationSecurity' 在类型'SafeUrl' 上不存在“,我该如何解决?

标签: javascriptangulartypescript

解决方案


您不应该使用该财产,它是私有财产。你应该只分配SafeUrl给你的profileImage

profileImage?: SafeUrl;

loadProfileImage(userID){
    this.settingsService.getProfileImage(userID).subscribe((data) =>{
        if (data){
            this.profileImage = this.sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL( data ));
        }
    });
}

您可以在模板中使用它,例如:

<img *ngIf="profileImage" [src]="profileImage">

注意:您也可以使用自定义的 SafeUrl 管道来为您执行此操作。结合async管道,您可以使代码更小


推荐阅读