我的服务定义如下:
据我所知,这个服务应该在整个应用程序的运行时是单例的。
现在,当页面刷新时,它正确地记录了“Called DataService constructor”。
不幸的是,当我导航到另一个页面(使用延迟加载模块的新路由)时,构造函数再次运行,并在每个路由更改时记录“Called DataService constructor”。 我错过了什么吗?
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, of, Subscription, combineLatest } from 'rxjs';
import { map, catchError, retry } from 'rxjs/operators';
@Injectable({
providedIn: 'root',
})
export class MyDataService {
constructor(private http: HttpClient) {
console.log('Called MyDataService constructor');
}
}
据我所知,这个服务应该在整个应用程序的运行时是单例的。
现在,当页面刷新时,它正确地记录了“Called DataService constructor”。
不幸的是,当我导航到另一个页面(使用延迟加载模块的新路由)时,构造函数再次运行,并在每个路由更改时记录“Called DataService constructor”。 我错过了什么吗?