繁体   English   中英

Angular 和 SVG:如何动态加载 SVG 组件?

[英]Angular and SVG: how to dynamically load SVG components?

在我正在开发的应用程序中,我有一个命令列表(GC->circle、GB->box、GE->ellipse 等)。 我必须在 SVG 中渲染它们。

我遵循了https://angular.io/guide/dynamic-component-loader指南,但我对 SVG 有一些遗漏。

我为每个命令准备了一个组件(这听起来很愚蠢,因为除了模板之外代码都相同):

@Component({
  selector: '[app-gc]',
  template: '<svg:circle [attr.r]="command.diameter/2" [attr.stroke]="command.borderColor" fill="#000" [attr.stroke-width]="command.borderThickness" />'
})
export class GCComponent implements OnInit, SVGComponent {
  @Input('[command]') command: ZPL2.GraphicShapeBase;
  constructor() { }
}

我加载渲染命令的组件如下所示:

@Component({
  selector: '[draggableSVGItem]',
  template: '<svg:g svg-host/>'
})
export class DraggableSvgItemComponent implements OnInit {      
  x: number = 0;
  y: number = 0;
  command: ZPL2.GraphicShapeBase;

  @Input()
  set draggableSVGItem(graphicCommand: ZPL2.GraphicShapeBase) {
    this.command = graphicCommand;
    this.x = graphicCommand.x;
    this.y = graphicCommand.y;
  }
  constructor() { }

  ngOnInit() {
    // Get the appropriate component for the command
    var componentType = null;
    if (this.command instanceof ZPL2.GC) {
      componentType = GCComponent;
    }
    if (this.command instanceof ZPL2.GB) {
      componentType = GBComponent;
    }
    if (this.command instanceof ZPL2.GE) {
      componentType = GEComponent;
    }

    // Get the component factory
    let componentFactory = this.componentFactoryResolver.resolveComponentFactory(componentType);

    // Clear the host view
    let viewContainerRef = this.svgHost.viewContainerRef;
    viewContainerRef.clear();

    // Dynamically create the component and set its command as the current one
    let componentRef = viewContainerRef.createComponent(componentFactory,);
    (<SVGComponent>componentRef.instance).command = this.command;
  }
}

一切都几乎完美无缺。 我遇到的问题是组件创建将 SVG 元素封装到 DIV 中,因此输出为空白:

额外的div

我是 Angular 的新手,你有什么建议可以摆脱那个 DIV 并使代码更简单吗?

编辑

我按照此链接中的示例解决了它: https : //www.chrisjmendez.com/2017/06/17/angular-dynamically-inserting-svg-into-an-element/

通过绑定元素的 innerHTML 属性,我可以根据命令类类型设置不同的 SVG 内容:

模板: <svg:g [innerHTML]="svg"/>

ngOnInit() {
  // Get the appropriate component for the command
  var html = ``;
  if (this.command instanceof ZPL2.GC) {
    html = `<circle r="` + this.command.diameter / 2 + `" stroke="` + this.command.borderColor + `" fill="#000" stroke-width="` + this.command.borderThickness + `" />`;
  }
  if (this.command instanceof ...

  this.svg = this.sanitizer.bypassSecurityTrustHtml(html);
}

这样我就不需要为每个命令创建一个组件。 每个命令都可以有自己的 getHTML() 方法。

缺点是我没有绑定命令的属性,对吧?

我的动态加载 svg 文件的解决方案(对于同一组件):

在 .html 模板中(我使用下拉列表实现了它,但在这里我剪切了示例代码):

<object *ngIf="currentSVG" type="image/svg+xml" [data]="SVGFile"></object>

在 .ts 文件中:

import { DomSanitizer, SafeUrl } from '@angular/platform-browser';

//component and class definition

SVGFile: SafeUrl;
currentSVG: string;

constructor(protected sanitizer: DomSanitizer) { ... }

//your code  

this.currentSVG = True;
this.SVGFile = this.sanitizer.bypassSecurityTrustResourceUrl('<your SVG file path>');

注意:SVG 文件应位于资产目录中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM