Angular 角度6:未调用它

Angular 角度6:未调用它,angular,typescript,Angular,Typescript,在angular 6应用程序中,我创建了重定向到ClosureFormComponent的路由,如下代码所示: export const closureRoutes: Routes = [ { path: 'closure', component: ClosureFormComponent } ]; 在“确认组件”对话框中,单击按钮,使用“路由”导航到组件: import { Router } from '@angular/router'; export class Confirmat

在angular 6应用程序中,我创建了重定向到ClosureFormComponent的路由,如下代码所示:

export const closureRoutes: Routes = [
    { path: 'closure', component: ClosureFormComponent }
];
在“确认组件”对话框中,单击按钮,使用“路由”导航到组件:

import { Router } from '@angular/router';
export class ConfirmationDialogComponent implements OnInit {

    constructor(private router: Router) { }

    ngOnInit() {
    }

    onContinueClick() {
        this.router.navigate(['/closure']);
    }

}
下面是我要导航到的组件:

import { Component, OnInit, NgZone, ChangeDetectorRef } from '@angular/core';
import { CaseServices } from '../../services/case-services.services'
import { ActivatedRoute } from '@angular/router';

@Component({
    selector: 'app-service-closure',
    templateUrl:'closure-form.component.html'
})
export class ClosureFormComponent implements OnInit {
    constructor(private route: ActivatedRoute, private zone: NgZone, private cdr: ChangeDetectorRef, private caseFormDataService: CaseServices) {

    }

    getClosureFormData() {
        this.caseFormDataService.loadClosureFormData(1, '15', 1);
    }

    ngOnInit(): void {
        this.zone.run(() => {
            console.log('closure form loaded');

            this.route.data.subscribe(() => {
                console.log('closure form loaded route');
            });
            this.cdr.detectChanges();
        });
    }
}
问题是Ngonit ClosureFormComponent并没有被触发,也并没有记录任何控制台语句。搜索后,我认为这可能是区域相关的问题,但仍然没有运气

编辑:对我来说奇怪的是,组件html显示在浏览器中,但控制台中没有语句。无法得到为什么会这样

您有:

onContinueClick() {
    this.router.navigate(['/closure']);
}
应该是

onContinueClick() {
    this.router.navigate(['closure']);
}

导航是一种相对路径路由

尝试类似的方法

onContinueClick() {
    this.router.navigateByUrl('/closure');
}
尽量不要使用NgZone


当你说Ngonit ClosureFormComponent没有被触发时,你的意思是如果你把console.log放在构造函数中,它就被触发了吗

视图如何,它是否在导航后显示新组件?将console.log直接放在ngOnInit中,而不是放在ngZone中。然后它工作了吗?请尝试从构造函数中删除所有注入,只需将console.log直接放在ngOnInityes.构造函数中,它就被激发了。使用或不使用ngZone也没有任何区别。