Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to import one SVG image into a typeScript/Angular?

I’m new to Angular/TypeScript.

In my project, I used icons and I have to replace the icons with images in SVG format.

I downloaded the image here.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Now, I need to import this SVG image into the typeScript. But, I don’t understand what I should do.

dashboard.component.ts

import { Component, OnInit } from '@angular/core';
import { Menu } from './types/menu';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.css'],
})
export class DashboardComponent implements OnInit {
  showSideBar: boolean = false;

  menus: Menu[] = [
    {
      name: 'Administration',
      iconClass: 'bx bx-lock-alt',
      active: false,
      submenu: [{ name: 'Portfolio', url: '/administration/portfolio' }],
    },
  ];

Do you know how I have to replace 'bx bx-lock-alt', with the SVG?

For information: the SVG image was saved in the following path <img src="assets/images/file.svg.

Here is a reproduction stackblitz.

Thank you very much.

>Solution :

Does this answer your question?

import { Component, OnInit } from '@angular/core';
import { Menu } from './types/menu';

@Component({
  selector: 'app-dashboard',
  templateUrl: './dashboard.component.html',
  styleUrls: ['./dashboard.component.css'],
})
export class DashboardComponent implements OnInit {
  showSideBar: boolean = false;

  menus: Menu[] = [
    {
      name: 'Administration',
      iconClass: 'file.svg',
      active: false,
      submenu: [{ name: 'Portfolio', url: '/administration/portfolio' }],
    },
  ];

  ngOnInit() {}

  toggle(index: number) {
    this.menus[index].active = !this.menus[index].active;
  }

  toggleSideBar() {
    this.showSideBar = !this.showSideBar;
  }

  selectMenu(parentMenu: { name: string }): void {
    this.menus.forEach((menu) => {
      if (menu.name !== parentMenu.name) {
        menu.active = false;
      } else {
        menu.active = !menu.active;
      }
    });
  }

  getSvg(menu: Menu) {
    return `assets/images/${menu.iconClass}`;
  }
}

stackblitz

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading