Naman Jain
Naman Jain

Reputation: 411

Angular 8: Is it possible to create only one instance of services inside shared module

I have a shared.module which has several services and pipes. I want to have only one instances of those services globally. If I import shared.module inside other modules, more than one instance were created. I also tried importing shared.module inside aap.module but it didn't work.

Upvotes: 0

Views: 298

Answers (1)

IAfanasov
IAfanasov

Reputation: 4993

It could be archived by providing service in the root without importing it into any module.

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

@Injectable({
  providedIn: 'root',
})
export class UserService {
}

Angular documentation has more details about providing scope.

Upvotes: 5

Related Questions