【发布时间】:2020-02-17 23:01:33
【问题描述】:
我的 html 文件中有这个调用:
profile.html
<div mat-card-avatar *ngIf="imgResult" [ngStyle]="{ 'background-image': 'url(' + imgResult + ')' }" class="avatar">
<mat-icon *ngIf="!imgResult">person</mat-icon>
</div>
我的组件是这样的:
profile.component.ts
import { DomSanitizer } from '@angular/platform-browser';
...
picture: File;
imgResult: any;
...
constructor(
private sanitizer: DomSanitizer
) { }
...
onChange(event) {
this.picture= event.target.files[0];
const reader = new FileReader();
reader.readAsDataURL(this.picture);
reader.onload = (ev: any) => {
this.imgResult = ev.target.result;
return this.sanitizer.bypassSecurityTrustUrl(this.imgResult);
};
}
图片加载正常,但我无法停止sanitizing unsafe URL value 警告。
我该怎么做?
【问题讨论】: