接收到的Base64图像中的换行符是问题的原因。这是我的解决方案:
//This goes to template <div>:
[style.background-image]="makeTrustedImage(images[i].file)"
//And this goes to component:
constructor(private domSanitizer: DomSanitizer) {}
makeTrustedImage(item) {
const imageString = JSON.stringify(item).replace(/\\n/g, '');
const style = 'url(' + imageString + ')';
return this.domSanitizer.bypassSecurityTrustStyle(style);
}
[style.background-image]="'url('+images[i].file+')' | safeResourceUrl"
(其中管道使用bypassSecurityTrustResourceUrl(value)
)。 - Günter Zöchbauer