简体   繁体   English

从 HTML 页面上的 SVG 文件渲染图标

[英]Render icons from an SVG file on HTML page

I have an icons.svg file that seems to contain assets that I need to use in my Angular app.我有一个icons.svg文件,它似乎包含我需要在 Angular 应用程序中使用的资产。

The file looks something like this (note, the ... means omitted data due to security reasons):该文件看起来像这样(注意, ...表示由于安全原因而省略的数据):

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><defs><path d="..." id="o"/></defs><symbol id="icon-add-member-contain-heavy" viewBox="0 0 64 64"><path d="B0 ... 4A3q"/></symbol><symbol id="icon-add-member-contain-light" viewBox="0 0 64 64"><path d="A5 ... 2i5m"

...
...
.../></symbol></svg>

I am trying to reference the file in my component HTML (as shown in the code below), but I don't know how to render an icon.我试图在我的组件 HTML 中引用该文件(如下面的代码所示),但我不知道如何呈现图标。 I am guessing I need to use the id 's that are shown in the above svg code, like id="icon-add-member-contain-heavy" ?我猜我需要使用上面 svg 代码中显示的id ,比如id="icon-add-member-contain-heavy"

    <div>
      <img src="../../src/assets/icons.svg">
    </div>

In angular, you should be able to render the svg as a component.在 angular 中,您应该能够将 svg 渲染为组件。 From the angular docs:来自 angular 文档:

import { Component } from '@angular/core';

@Component({
  selector: 'app-svg',
  templateUrl: './svg.component.svg',
  styleUrls: ['./svg.component.css']
})
export class SvgComponent {
  fillColor = 'rgb(255, 0, 0)';

  changeColor() {
    const r = Math.floor(Math.random() * 256);
    const g = Math.floor(Math.random() * 256);
    const b = Math.floor(Math.random() * 256);
    this.fillColor = `rgb(${r}, ${g}, ${b})`;
  }
}

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

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