问题描述
我想将base64编码的SVG图像转换为扩展名为.svg的Angular模板,例如my-template.component.svg。
data:image/svg+xml;base64,PHN2ZyBpZD0iREVWSUNFX01BUF8tX09wZW5lZF9jb3B5IiBkYXRhLW5...
这应该成为普通的SVG文件,我想将其用作模板。因此my-template.component.svg
的内容应如下所示:
<svg width = "100" height = "100">
<circle cx = "50" cy = "50" r = "40" stroke = "green" stroke-width = "4" fill = "yellow" />
...
</svg>
否则,由于选择器在DOM中不可用,因此我无法使用CSS处理base64编码的SVG。
<img id ="img" [src]="api-url +'/path/to/svg/file.svg'" />
<img id="img" src="data:image/svg+xml;base64,PHN2ZyBpZD0iREVWSUNFX01BUF8tX09wZW5lZF9jb3B5IiBkYXRhLW5..." />
解决方法
您的API返回base64编码的数据URL。 您可以将编码后的部分(在“ data:image / svg + xml; base64”之后)并在base64编码器/解码器中使用:
获得的SVG XML can be used in Angular模板(8个以上的CLI)