angular2解析器多次实例化
我对多次实例化Angular2解析器有问题 这是我的功能模块声明(由路由器延迟加载): 这是有问题的解析器的代码(我们称之为ResolverA): 问题是,这个解析器被实例化了两次(并且只在一个实例中解析)。第二个实例(注入到组件中)具有未解析的数据。事实上,第二个实例甚至不应该存在,解析器应该是单例的 在这种情况下,控制台输出:angular2解析器多次实例化,angular,typescript,angular2-routing,Angular,Typescript,Angular2 Routing,我对多次实例化Angular2解析器有问题 这是我的功能模块声明(由路由器延迟加载): 这是有问题的解析器的代码(我们称之为ResolverA): 问题是,这个解析器被实例化了两次(并且只在一个实例中解析)。第二个实例(注入到组件中)具有未解析的数据。事实上,第二个实例甚至不应该存在,解析器应该是单例的 在这种情况下,控制台输出: Angular 2 is running in the development mode. Call enableProdMode() to enable the p
Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
Resource NEW
Resource RESOLVE
Resource NEW //THIS SHOULDN'T BE HERE
Reasons: undefined
Reasons: undefined
在angular 2.1.2中按预期工作,但在angular 2.2.4中不起作用。
Typescript版本:2.0.10
我的代码是否有问题(自angular 2.2以来发生了变化),或者这被认为是angular中的一个bug?关于延迟加载模块中的提供程序被多次实例化,存在一个已知问题
import {Resolve, ActivatedRouteSnapshot, RouterStateSnapshot} from "@angular/router";
import {Injectable} from "@angular/core";
import {Observable} from "rxjs";
import {AuthHttp} from "angular2-jwt";
@Injectable()
export class ResolverA implements Resolve<IDataDto> {
private API_URL = '...';
private _reasons: IDataDto[];
constructor(private http: AuthHttp) {
console.log("Resource NEW");
}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any>|Promise<any>|any {
console.log("Resource RESOLVE");
return this.http.get(this.API_URL)
.map(result => result.json())
.do(result => this._reasons = result);
}
get reasons(): IDataDto[] {
return this._reasons;
}
}
export interface IDataDto {
reasonCode: string,
description: string,
gainLossType: GainLossType
}
export type GainLossType = "GAIN" | "LOSS";
// All needed imports
@Component({
selector: 'componentA',
template: require('./componentA.template.html')
})
export class ComponentA implements OnInit {
constructor(private router: Router,
private timeResolver: TimeResolver,
private resolverA: ResolverA,
private messageService: MessageService) {
}
ngOnInit(): void {
// other UI initialization code
this.initData();
}
private initData() {
let reasons = this.resolverA.reasons; //UNDEFINED!
console.log("Reasons:", reasons);
}
}
Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
Resource NEW
Resource RESOLVE
Resource NEW //THIS SHOULDN'T BE HERE
Reasons: undefined
Reasons: undefined