Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace NgModule approach by provide function #336

Open
rlexa opened this issue Nov 29, 2024 · 1 comment
Open

Replace NgModule approach by provide function #336

rlexa opened this issue Nov 29, 2024 · 1 comment

Comments

@rlexa
Copy link

rlexa commented Nov 29, 2024

Angular moved on to functional providers approach (EnvironmentProviders), please replace NgModule by an appropriate provide function.

Right now it has to be provided with a helper function from Angular:

bootstrapApplication(AppComponent, {
    providers: [
      importProvidersFrom(ApiModule.forRoot({rootUrl: BACKEND_PATH})),
...

Instead the code could be generated like:

@Injectable({providedIn: 'root'})
export class ApiConfiguration {
  rootUrl?: string;
}

export const apiConfigurationGenerated: ApiConfiguration = {rootUrl: '/api'};

export function provideNgOpenapi(params: ApiConfigurationParams) {
  return makeEnvironmentProviders([
    SomeGeneratedService,
    OtherGeneratedService,
    {provide: ApiConfiguration, useValue: {...apiConfigurationGenerated, ...params}},
  ]);
}

With environment providers there is no need for already-exists checking. This allows for modern approach:

bootstrapApplication(AppComponent, {
    providers: [
      provideNgOpenapi({rootUrl: BACKEND_PATH}),
...
@mumenthalers
Copy link

@rlexa missed this issue and created a separate one #342, with the same goal but a non-breaking approach by using a standalone option.
Naming can be discussed, but for sure it is the logical way to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants