2

I have bootstrapped an Angular v18 workspace with an application and a library. I've created a service in the library and wanted to use that in the application. Everything organized with the standalone approach, without NgModules.

Previously I've registered the providers in the library's NgModule and was importing it in the application with the module's forRoot(...) method. How can I achieve the same without NgModules (using the standalone approach)?

1 Answer 1

0

You have to use importProvidersFrom to add them to the providers array of bootstrapApplication.

import { bootstrapApplication } from '@angular/platform-browser';
...

...
bootstrapApplication(App, {
  providers: [
    importProvidersFrom([
       ...
       YourModule.forRoot(),
       ...
    ]),
  ]
});
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, but this assumes that providers in the library are organized to a NgModule and use them by calling the .forRoot() method. Is the same result reachable without NgModule?
@MiklósSzabó do you mean it's standalone? which library, could you give a reference. You could try adding the services directly to the providers array, of either bootstrapApplication or AppComponent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.