首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角- Img src属性不呈现SVG。

角- Img src属性不呈现SVG。
EN

Stack Overflow用户
提问于 2022-06-02 05:02:32
回答 1查看 1.1K关注 0票数 0

我有一个来自后端的SVG文件,当我试图在前端角(V11)应用程序中显示它时,它不会呈现,看起来也坏了。

这是我的代码:

代码语言:javascript
复制
<div fxLayout="column" *ngFor="let svgFile of activePageDataChunk">
    <img src="data:image/svg+xml,svgFile">

如下所示:

这是SVG中的内容:

代码语言:javascript
复制
<svg xmlns="http://www.w3.org/2000/svg" width="420mm" height="297mm">
  <rect width="100%" height="100%" id="paperBorder" fill="#FFF"/>
  <svg id="margins" x="5mm" y="5mm">
    <svg x="38.200050592422485mm" overflow="visible">
      <svg class="level_marker" x="0mm" y="28.509538173675537mm" overflow="visible">
        <text class="level_marker_text" font-size="12" y="-6" x="6mm" text-anchor="start">+0.000</text>
        <line x1="0" y1="0" x2="20mm" y2="0" fill="none" stroke="#231f20" stroke-miterlimit="10"/>
        <line x1="0" y1="0" x2="2.5mm" y2="2.5mm" fill="none" stroke="#231f20" stroke-miterlimit="10"/>
        <line x1="2.5mm" y1="2.5mm" x2="5mm" y2="0mm" fill="none" stroke="#231f20" stroke-miterlimit="10"/>
      </svg>
      <svg class="level_marker" x="0mm" y="-2.490461826324463mm" overflow="visible">
        <text class="level_marker_text" font-size="12" y="-6" x="6mm" text-anchor="start">+3.100</text>
        <line x1="0" y1="0" x2="20mm" y2="0" fill="none" stroke="#231f20" stroke-miterlimit="10"/>
        <line x1="0" y1="0" x2="2.5mm" y2="2.5mm" fill="none" stroke="#231f20" stroke-miterlimit="10"/>
        <line x1="2.5mm" y1="2.5mm" x2="5mm" y2="0mm" fill="none" stroke="#231f20" stroke-miterlimit="10"/>
      </svg>
    </svg>
  </svg>
  <svg x="0mm" y="195mm" width="83mm" height="22mm">
    <rect width="100%" height="100%" fill="none" stroke="#666"/>
    <text x="1mm" y="2.5mm" font-size="2mm" font-family="DINPro-Light, DIN Pro" font-weight="300" style="isolation:isolate"/>
    <text x="1mm" y="7.5mm" font-size="4.5mm" font-family="DINPro, DIN Pro" style="isolation:isolate"/>
  </svg>
  <svg x="0mm" y="247mm" width="27.5mm" height="10mm">
    <rect width="100%" height="100%" fill="none" stroke="#666"/>
    <text x="1mm" y="2.5mm" font-size="2mm" font-family="DINPro-Light, DIN Pro" font-weight="300" style="isolation:isolate">DATE</text>
    <text x="1mm" y="7.5mm" font-size="4.5mm" font-family="DINPro, DIN Pro" style="isolation:isolate">2.6.2022</text>
  </svg>
  <svg x="0mm" y="257mm" width="83mm" height="10mm">
    <rect width="100%" height="100%" fill="none" stroke="#666"/>
    <text x="1mm" y="2.5mm" font-size="2mm" font-family="DINPro-Light, DIN Pro" font-weight="300" style="isolation:isolate">SCALE</text>
    <text x="50%" y="50%" alignment-baseline="central" text-anchor="middle" font-size="4.5mm" font-family="DINPro, DIN Pro" style="isolation:isolate">1:100</text>
  </svg>
  <svg x="0mm" y="277mm" width="83mm" height="10mm">
    <rect width="100%" height="100%" fill="none" stroke="#666"/>
    <text x="1mm" y="2.5mm" font-size="2mm" font-family="DINPro-Light, DIN Pro" font-weight="300" style="isolation:isolate">DRAWN BY</text>
    <text x="1mm" y="7.5mm" font-size="4.5mm" font-family="DINPro, DIN Pro" style="isolation:isolate"/>
  </svg>
</svg>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-02 05:58:29

正如注释中所讨论的,提供的数据是SVG元素,因此不能将值绑定到<img>元素的src。

有两种方法可以做到:

  1. 将SVG元素转换为base64字符串,以便您可以使用<img>元素。
  2. 使用DOMSanitizer将SVG元素绑定到HTML。

答案1

若要将SVG文件转换为base64字符串,请考虑以下问题:将内联SVG转换为Base64字符串

代码语言:javascript
复制
import { DomSanitizer } from '@angular/platform-browser';

constructor(private _sanitizer: DomSanitizer) {}

getSVGImageUrl(image) {
  let base64string = btoa(image);
  return this._sanitizer.bypassSecurityTrustResourceUrl(
    `data:image/svg+xml;base64,${base64string}`
  );
}
代码语言:javascript
复制
<div *ngFor="let svgFile of activePageDataChunk">
  <img [src]="getSVGImageUrl(svgFile)" />
</div>

示例StackBlitz演示(答案1)

答案2

代码语言:javascript
复制
import { DomSanitizer } from '@angular/platform-browser';

constructor(private _sanitizer: DomSanitizer) {}

getSVGImage(image) {
  return this._sanitizer.bypassSecurityTrustHtml(`${image}`);
}
代码语言:javascript
复制
<div *ngFor="let svgFile of activePageDataChunk">
  <div [innerHTML]="getSVGImage(svgFile)"></div>
</div>

示例StackBlitz演示(答案2)

参考

警告:消毒不安全样式值url (类似问题)

bypassSecurityTrustResourceUrl()

bypassSecurityTrustHtml()

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72470896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档