有一个服务trackUrlService
被导入到CoreModule
export class TrackUrlService implements OnDestroy {
...
constructor(private router: Router, private route: ActivatedRoute) {
...
this.subs.add(
this.route.params.subscribe((params: Params) => {
console.log(params.workflowId); // output: undefined
...
})
);
}
...
有一个组件DummyComponent
ngOnInit() {
this.subs.add(
this.trackUrl.currWorkflowId.subscribe(currWorkflowId => {
console.log(currWorkflowId); // output: undefined
}),
...
/* this.route.params.subscribe(params => {
console.log(params.workflowId) // output: 391 (correct)
}), */
...
问题:
this.route.params
为什么从服务的构造函数输出调用undefined
?
this.route.params
但是从组件的工作中调用ngOnInit()
并输出正确的值。