javascript - 角度 -“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' 上不存在“,我该如何解决?
解决方案
您不应该使用该财产,它是私有财产。你应该只分配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
管道,您可以使代码更小
推荐阅读
- amazon-web-services - 在 Terraform 中找到 aws-elasticbeanstalk-ec2-role?
- jms - Message Broker 将设计与 IBM MQ 集成
- git - 如何处理 gitmodules 中过期的 url
- mysql - 如何在sql中计算具有相同id的每个值
- azure - 创建 Azure 策略问题
- django - 用 celery 和 beautifulsoup 提取数据
- python - cronjob中的python脚本没有将标准输出写入文件
- r - 如何将数据框多次复制到列表中
- python - 在 docker 容器中运行时无法将 geopandas 导入 python
- powershell - 通过 ::1 端口 22 重置连接(Windows 10 + PowerShell/cmd + OpenSSH)