Well now v5 didnt have scss so what is the correct way to integrate and consume this in angular ?
In _app.module.ts_
import { NgModule, /** ADD THIS -> **/ CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
@NgModule({
declarations: [...],
entryComponents: [...],
imports: [...],
exports: [...],
providers: [...],
schemas: [CUSTOM_ELEMENTS_SCHEMA] /** 馃憟馃徎 ADD THIS **/
})
export AppModule {}
in _src\index.html_
<body>
<app-root></app-root>
<script src="https://unpkg.com/[email protected]/dist/ionicons.js"></script>
</body>
Recompile/ Re-serve
Thanks for the answer that works but is there a way to use it this way?
https://stackoverflow.com/questions/60133077/ionicons-version-5-with-angular
While this works, it is not the Angular way to include scripts into index.html. I hope there is a cleaner way.
Does this mean we need to change all to
As @bleuscyther mentions, you can integrate the script in your index.html
. There are two flaws in my view about this: It is 1) not the Angular way to integrate scripts in index.html
and 2) it links an externally hosted script.
Number 2) can be fixed by locally installing Ionicons 5 with npm. But data within node_modules
cannot be linked from index.html
, so we need a script that copies the whole ionicons
folder from node_modules
into our application's assets.
This can be achieved as follows by modifying angular.json
. Find the assets
array in the build
and test
objects, then add the following lines:
{
"glob": "**/*",
"input": "./node_modules/ionicons",
"output": "./assets/ionicons"
}
Then just link in index.html
the right file. It should be: <script src="assets/ionicons/dist/ionicons/ionicons.esm.js" type="module"></script>
Number 1) seems to be tricky. After following the guide about integrating Stencil into an Angular app, I found that this approach works. Open main.ts
in your Angular project and add the following code at the end:
import { applyPolyfills, defineCustomElements } from "ionicons/dist/loader";
applyPolyfills().then(() => {
defineCustomElements(window, {
resourcesUrl: "assets/ionicons/"
});
});
This will basically tell the Ionicons script to look for the svg
directory within assets/ionicons/
. To be honest I don't know exactly why and I couldn't find documentation about it. Maybe someone from the Ionicon team can tell me if there is a better way to do this.
Anyway, all you then have to do is make sure that the svg
directory is actually copied into your assets
. I did this with a similar edit in angular.json
as in the number 2) above:
{
"glob": "**/*",
"input": "./node_modules/ionicons/dist/svg",
"output": "./assets/ionicons/svg"
}
This will only copy the svg
folder into the assets, because that is all we will need from there.
import * as icons from 'ionicons/icons';
import { DomSanitizer } from '@angular/platform-browser';
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'page-icons',
templateUrl: './icons.component.html',
})
export class IconsComponent implements OnInit {
icons: = icons;
constructor(private domSanitizer: DomSanitizer) {}
}
<img [src]="domSanitizer.bypassSecurityTrustUrl(newIcons.heart)">
But can't control icon's (image) color.
The color seems like a separate issue (#784 ) It works for icons that do not have a hard-coded stroke color.
As @bleuscyther mentions, you can integrate the script in your
index.html
. There are two flaws in my view about this: It is 1) not the Angular way to integrate scripts inindex.html
and 2) it links an externally hosted script.Number 2) can be fixed by locally installing Ionicons 5 with npm. But data within
node_modules
cannot be linked fromindex.html
, so we need a script that copies the wholeionicons
folder fromnode_modules
into our application's assets.This can be achieved as follows by modifying
angular.json
. Find theassets
array in thebuild
andtest
objects, then add the following lines:{ "glob": "**/*", "input": "./node_modules/ionicons", "output": "./assets/ionicons" }
Then just link in
index.html
the right file. It should be:<script src="assets/ionicons/dist/ionicons/ionicons.esm.js" type="module"></script>
Number 1) seems to be tricky. After following the guide about integrating Stencil into an Angular app, I found that this approach works. Open
main.ts
in your Angular project and add the following code at the end:import { applyPolyfills, defineCustomElements } from "ionicons/dist/loader"; applyPolyfills().then(() => { defineCustomElements(window, { resourcesUrl: "assets/ionicons/" }); });
This will basically tell the Ionicons script to look for the
svg
directory withinassets/ionicons/
. To be honest I don't know exactly why and I couldn't find documentation about it. Maybe someone from the Ionicon team can tell me if there is a better way to do this.Anyway, all you then have to do is make sure that the
svg
directory is actually copied into yourassets
. I did this with a similar edit inangular.json
as in the number 2) above:{ "glob": "**/*", "input": "./node_modules/ionicons/dist/svg", "output": "./assets/ionicons/svg" }
This will only copy the
svg
folder into the assets, because that is all we will need from there.
When I do this, I do see the icon using <ion-icon name="heart"></ion-icon>
but I also get an error inside cli 'ion-icon' is not a known element:
Any idea how to fix it?
As @bleuscyther mentions, you can integrate the script in your
index.html
. There are two flaws in my view about this: It is 1) not the Angular way to integrate scripts inindex.html
and 2) it links an externally hosted script.Number 2) can be fixed by locally installing Ionicons 5 with npm. But data within
node_modules
cannot be linked fromindex.html
, so we need a script that copies the wholeionicons
folder fromnode_modules
into our application's assets.This can be achieved as follows by modifying
angular.json
. Find theassets
array in thebuild
andtest
objects, then add the following lines:```
{
"glob": "*/",
"input": "./node_modules/ionicons",
"output": "./assets/ionicons"
}
```
Then just link in
index.html
the right file. It should be:<script src="assets/ionicons/dist/ionicons/ionicons.esm.js" type="module"></script>
Number 1) seems to be tricky. After following the guide about integrating Stencil into an Angular app, I found that this approach works. Open
main.ts
in your Angular project and add the following code at the end:```
import { applyPolyfills, defineCustomElements } from "ionicons/dist/loader";
applyPolyfills().then(() => {
defineCustomElements(window, {
resourcesUrl: "assets/ionicons/"
});
});
```
This will basically tell the Ionicons script to look for the
svg
directory withinassets/ionicons/
. To be honest I don't know exactly why and I couldn't find documentation about it. Maybe someone from the Ionicon team can tell me if there is a better way to do this.Anyway, all you then have to do is make sure that the
svg
directory is actually copied into yourassets
. I did this with a similar edit inangular.json
as in the number 2) above:```
{
"glob": "*/",
"input": "./node_modules/ionicons/dist/svg",
"output": "./assets/ionicons/svg"
}
```
This will only copy the
svg
folder into the assets, because that is all we will need from there.When I do this, I do see the icon using
<ion-icon name="heart"></ion-icon>
but I also get an error inside cli'ion-icon' is not a known element:
Any idea how to fix it?
Doesn鈥檛 the second post in this issue fix it? It did for me.
Yeah, it worked. But I think this is still not ready for using in the latest angular.
The solution from @bleuscyther works fine, but mine not, which I consider the same, but from local node_modules
:
"scripts": [
"node_modules/ionicons/dist/ionicons.js"
]
Got this error instead:
GET http://localhost:4200/ionicons/ionicons.esm.js net::ERR_ABORTED 404 (Not Found)
Just add this to the assets in angular.json, and everything works.
`{
"glob": "**/*.svg",
"input": "node_modules/ionicons/dist/ionicons/svg",
"output": "./svg"
},`
Most helpful comment
As @bleuscyther mentions, you can integrate the script in your
index.html
. There are two flaws in my view about this: It is 1) not the Angular way to integrate scripts inindex.html
and 2) it links an externally hosted script.Number 2) can be fixed by locally installing Ionicons 5 with npm. But data within
node_modules
cannot be linked fromindex.html
, so we need a script that copies the wholeionicons
folder fromnode_modules
into our application's assets.This can be achieved as follows by modifying
angular.json
. Find theassets
array in thebuild
andtest
objects, then add the following lines:Then just link in
index.html
the right file. It should be:<script src="assets/ionicons/dist/ionicons/ionicons.esm.js" type="module"></script>
Number 1) seems to be tricky. After following the guide about integrating Stencil into an Angular app, I found that this approach works. Open
main.ts
in your Angular project and add the following code at the end:This will basically tell the Ionicons script to look for the
svg
directory withinassets/ionicons/
. To be honest I don't know exactly why and I couldn't find documentation about it. Maybe someone from the Ionicon team can tell me if there is a better way to do this.Anyway, all you then have to do is make sure that the
svg
directory is actually copied into yourassets
. I did this with a similar edit inangular.json
as in the number 2) above:This will only copy the
svg
folder into the assets, because that is all we will need from there.