Issue with IonIcons Not Displaying in Ionic Angular Standalone Components

831 Views Asked by At

I am currently working on an Ionic Angular project that utilizes standalone components. I've encountered an issue where IonIcons are not rendering properly in my templates. My project is built with Angular 17 and Ionicons 7.

The following icon usage in my template results in a warning:

<ion-icon name="log-out-outline" slot="start"></ion-icon>

The warning displayed is:

[Ionicons Warning]: Could not load icon with name "log-out-outline". Ensure that the icon is registered using addIcons or that the icon SVG data is passed directly to the icon component.

As a workaround, I tried manually importing the icons like this:

import { camera } from 'ionicons/icons';
//...
export class HomePageComponent implements OnInit {
  camera = camera;
  // ...
}

// In template
<ion-icon [icon]="camera" slot="icon-only" fill="outline"></ion-icon>

While this approach works, it's not ideal, especially when dealing with numerous icons, as it makes the code less clean and more difficult to manage.

I suspect that there might be a need for a specific import or configuration to properly use IonIcons in this setup. Has anyone faced a similar issue or can provide guidance on how to correctly import and use IonIcons in an Ionic Angular project with standalone components?

2

There are 2 best solutions below

0
Chris.Z On

add

addIcons({logOutOutline});

in your component's constructor.

0
Supun Viduranga On

you need to import

import { logOutOutline } from 'ionicons/icons';

and add this code inside the constructor

constructor() {
    addIcons({ logOutOutline });
  }

you can add multiple icons by separating them with a comma in both import section and the constructor.

use this code inside the HTML as follows

<ion-icon name="log-out-outline"></ion-icon>