ngneat / svg-icon

👻 A lightweight library that makes it easier to use SVG icons in your Angular Application
https://netbasal.com
MIT License
260 stars 35 forks source link

Error: inject() must be called from an injection context (SvgIconRegistry issue) #39

Closed denysSlobodianykAVA closed 3 years ago

denysSlobodianykAVA commented 3 years ago

[x] Regression (a behavior that used to work and stopped working in a new release)
[x] Support request

Current behavior

Updated version to 3.1.1 (from 1.0.0) and got next error: Screenshot 2021-04-26 at 21 44 08

AppModule

....
imports: [
    SvgIconsModule.forRoot({ icons: ICONS }), 
    // ICONS = [ {name: 'test', data: '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20"></svg>' } ] 
],
providers: [
 SvgIconRegistry
// Also tried { provide: SvgIconRegistry, useClass: SvgIconRegistry }
]
...

Just attached like this. And got an error. This occurs in that place: (ngneat-svg-icon.js:16)

...
class SvgIconRegistry {
    constructor(config) {
        this.svgMap = new Map();
        this.document = inject(DOCUMENT); // here, issue with 'inject'
        if (config.icons) {
            this.register(config.icons);
        }
    }
    ....

Also, use it inside the shared module:

...
imports: [
SvgIconsModule.forChild(getIcons())
],
exports: [
SvgIconsModule
]
...

But even when removed from the shared module - still an error.

Expected behavior

Minimal reproduction of the problem with instructions

What is the motivation/use case for changing the behavior?

Environment

Browser:

  • Chrome (desktop) version Version 90.0.4430.85 (Official Build) (arm64)

For Tooling issues:

  • Node version: v14.16.0
  • Platform: Mac

Other:

NetanelBasal commented 3 years ago

Can you reproduce it on stackblitz, please?

denysSlobodianykAVA commented 3 years ago

@NetanelBasal yes. https://stackblitz.com/edit/angular-ivy-d2mmak?file=src/app/app.module.ts

https://angular-ivy-d2mmak.stackblitz.io/ (inside console I got error)

denysSlobodianykAVA commented 3 years ago

@NetanelBasal Hi. Thanks for the fix. I see that version of the library wasn't changed. After 'npm i' v3.1.1 this issue still exists. So, need to wait for the new version with that fix?

NetanelBasal commented 3 years ago

3.2.0