gpt4 book ai didi

angular - 在Angular中显示带有img标签的SVG

转载 作者:行者123 更新时间:2023-12-03 04:29:51 29 4
gpt4 key购买 nike

我想从api获取svg并使用image标签显示它。我尝试了以下操作,但不起作用

public svgIcon: any;

public getRequest(url: string) {
const headers = new HttpHeaders().set('Accept', 'image/svg+xml');
this.http.get(url, { headers: headers, responseType: 'blob') }).subscribe({
const blob = new Blob([svg], {type: 'image/svg+xml'});
const url = URL.createObjectURL(blob);
URL.revokeObjectURL(url), {once: true}
this.svgIcon = url;
})
}


并在HTML中:

<img [src]="this.svgIcon">

最佳答案

修改您的订阅部分,如下所示:

public svgIcon: any;

public getRequest(url: string) {
const headers = new HttpHeaders().set('Accept', 'image/svg+xml');
this.http.get(url, { headers: headers, responseType: 'blob') }).subscribe({
const blob = new Blob([response], {type: 'image/svg+xml'});
const url = URL.createObjectURL(blob);
this.svgIcon= this.sanitizer.bypassSecurityTrustUrl(url);
})
}


并在您的HTML中

<img [src]='svgIcon'>

关于angular - 在Angular中显示带有img标签的SVG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59528823/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com