What is the best way to share services across Modules in angular2

0 votes
i want know with the help of an exmaple that What is the best way to share services across Modules in angular2?
4 days ago in Angular by Nidhi
• 10,860 points
24 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

In Angular 2+, it is best to share services between modules by:

Export the service in a Core or Shared module: Build a CoreModule (for singleton services) or SharedModule (for non-singleton services) and list the service in the providers array of such a module.

Use providedIn: 'root': When the service is a singleton, use the @Injectable({ providedIn: 'root' }) decorator within the service. This provides the service application-wide without having to add it to a module's providers array.

Lazy-loaded modules: For service that is particular to a feature module, implement them directly in the feature module. But with care because they can lead to multiple instances when the module is lazy-loaded.

Example:

@Injectable({

  providedIn: 'root' // Singleton service available app-wide

})

export class MyService {}

answered 4 days ago by Tanyaa

edited 3 days ago

Related Questions In Angular

0 votes
1 answer

What is the service that is used to configure the UI-Router and ngRoute in AngularJs?

Routing comes into play whenever you want ...READ MORE

answered Feb 11, 2020 in Angular by anonymous
• 82,840 points
650 views
0 votes
1 answer

What is the use of $eval in alert()?

Hey, kartik!! There is not much difference in ...READ MORE

answered Feb 4, 2020 in Angular by Niroj
• 82,840 points
1,493 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
0 answers

How do you use forkJoin() for parallel API calls?

can you explain me with the help ...READ MORE

4 days ago in Angular by Nidhi
• 10,860 points
21 views
0 votes
0 answers

What are the differences between mergeMap, concatMap, and switchMap?

can someone explain me What are the ...READ MORE

4 days ago in Angular by Nidhi
• 10,860 points
38 views
0 votes
0 answers

How does takeUntil() help in cleaning up Observables?

i want know with the help of ...READ MORE

4 days ago in Angular by Nidhi
• 10,860 points
25 views
0 votes
0 answers

How do you apply transform functions in PipeTransform Interface?

i want know with the help of ...READ MORE

4 days ago in Angular by Nidhi
• 10,860 points
29 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP