Angular-Dynamic-Hooks / ngx-dynamic-hooks

Automatically insert live Angular components into dynamic strings (based on their selector or any pattern of your choice) and render the result in the DOM.
MIT License
115 stars 6 forks source link

Angular 14 standalone components support #23

Open ilkerkoc opened 2 years ago

ilkerkoc commented 2 years ago

Hi.

I want to remove my components from app ngmodule or any module in v14 via simple standalone option.

How can we move

const componentParsers: Array = []

DynamicHooksModule.forRoot({})

in app.module

to new file with new shiny standalone components feature.

Thank you.

MTobisch commented 2 years ago

I haven't yet delved deeply into Angular v14's standalone components, but from what I gather, you have to register global providers/services in bootstrapApplication while components/directives/pipes are imported in the standalone components themselves.

Have you tried the following (not testet):

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

bootstrapApplication(AppComponent, {
  providers: [
    importProvidersFrom(DynamicHooksModule.forRoot({
      globalParsers: componentParsers
    })),
  ]
});

And then in your standalone component:

@Component({
  standalone: true,
  selector: 'app-root',
  imports: [
    DynamicHooksModule
  ],
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  [...]
}
kumiega commented 6 months ago

Hi @MTobisch, I can confirm that I have tested this example and it works fine without any errors. The only thing to remember is to import DynamicHooksModule inside the standalone component where <ngx-dynamic-hooks> will be used.

MTobisch commented 2 weeks ago

Little update!

I'm please to announce that Version 3 of the library has just released which uses the standalone components structure by default.

Instead of the old importProvidersFrom(DynamicHooksModule.forRoot()), you can now simply call provideDynamicHooks().

Then just import the standalone DynamicHooksComponent whereever you want to use <ngx-dynamic-hooks> like this:

@Component({
  ...
  imports: [DynamicHooksComponent]
})
export class YourComponent {
...
}