Why constructor of two root service does not work?

Gamov

Here is two root services:

Service 1:

@Injectable({
  providedIn: 'root',
})
export class DataService {
  private data$ = new BehaviorSubject([]);
  public dataObs$ = this.data$.asObservable();

  constructor(private http: HttpClient) {}

  setData(data: any) {
    this.data$.next(data);
  }

  loadData(url: string, category: number) {
    return this.http.get(`${url}?chapter=${category}`);
  }
}

Service 2:

@Injectable({
  providedIn: 'root',
})
export class ImportExportService {
  private data$: Observable<any>;

  constructor(private dataService: DataService) {
    this.data$ = this.dataService.dataObs$;
    this.data$.subscribe((e) => console.log(e));
    alert('Done');
  }
}

Why constructor of the second service does not work, I dont get message alert('Done');.

Both are registered as root service

Eli Porush

Angular create an instance of service only if it's injected somewhere in your components ( or other classes such as guards, pipes, etc ) . if you didn't inject it in any constructor, no insatnce of that service will be created. for more see Angular Docs

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

why does a constructor work this way?

Why service does not work "immediately"?

Fallback variadic constructor - why does this work?

Why constructor does not work in some cases?

Why does default copy constructor not work for this class

integer square root of an integer, why does this work?

Why kubectl exec --username=root does not work?

Why default constructor does not work when parameterized constructor added?

Passing class to constructor, when no such constructor exists. Why does it work?

Why Angular js Service does not work?

Why does session_start() function does not work in php constructor?

Why does this two's complement shortcut work?

Why does not work two controller in Angular JS?

Why does service runs as root while USERNAME is set to something else

Why does adding this constructor cause my API to work in C#?

C++ - Why does the default constructor work even in private inheritance

Why does this work in a for loop but not forEach? (array is not a constructor error)

why must use '*p=a', and use 'p=&a' does not work in constructor

Why pop to root does not work in this sample code SwiftUI?

setuid root does not work

Why does not MapHttpAttributeRoutes work in Stateless Web API in service fabric?

Why does bash parameter expansion not work inside systemd service files?

Why does using the service command work on a systemd distro?

Why does Unity (DI) work in the Controller, but not in my service layer?

Why does Scoped service resolve as two different instances for same request?

Why is my service running as root

Why does this recursive Java method for counting two characters work?

Why does my statement asking if two variables are equal not work?

working with two transactions, one seem to work but the other does not. why?