Active路由在服务的构造函数中未定义

问题描述 投票:0回答:1

有一项服务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()有效并且输出正确的值。

angular angular-routing angular-router angular-activatedroute
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.