如何在angular中向图像src插入base64字符串

如何在angular中向图像src插入base64字符串,angular,Angular,在我的config.ts中 export const imageBase64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAiAAA...." 在我的梦中 import { DomSanitizer, SafeUrl } from '@angular/platform-browser'; import { imageBase64 } from './config' ... myImg: SafeUrl; constructor

在我的config.ts中

export const imageBase64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAiAAA...." 
在我的梦中


import { DomSanitizer, SafeUrl } from '@angular/platform-browser';
import { imageBase64 } from './config'
...
  myImg: SafeUrl; 
  constructor(private sanitizer: DomSanitizer){
     this.myImg= this.sanitizer.bypassSecurityTrustUrl(imageBase64 );
  }
在我的html中

<img [src]="myImg" alt="" />

不幸的是,此操作失败,错误消息
net::ERR\u无效\u URL


然后我使用
绕过securitytrustresourceurl
也失败了,这有什么错

可以使用插值:
src={{{myImg}


请参阅下面的JSFIDLE以获得帮助:

使用
get
属性返回url

public get myImg(): string {
  return this.sanitizer.bypassSecurityTrustUrl(imageBase64 );
}

很好。尝试演示并找出错误所在

<img [src]="imageBase64" alt="">


演示:

我们不需要清理base64数据。 我们可以使用字符串插值来显示以字符串形式存在的任何数据

<img src={{ imageBase64}} alt="" />


请在处共享imageBase64值。
imageBase64
是否为有效数据?bcs您的代码在JSFIDLE中运行良好,具有正确的base 64数据
console.log(imageBase64)
?在使用消毒液之前?非常感谢,base64数据是错误的,导致了错误,不需要对base64数据进行消毒。