在我的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();
});
}
}
问题是未触发ngOnInit()(ClosureFormComponent),并且未记录任何控制台语句。搜索后,我认为这可能是与区域相关的问题,但还是没有运气!。
编辑:对我来说,很奇怪的是,组件(html)显示在浏览器中,但控制台中没有语句。无法得到为什么呢?
答案 0 :(得分:1)
您有:
onContinueClick() {
this.router.navigate(['/closure']);
}
应该是
onContinueClick() {
this.router.navigate(['closure']);
}
navigate()
是相对路径路由
答案 1 :(得分:0)
尝试这样的事情
onContinueClick() {
this.router.navigateByUrl('/closure');
}
答案 2 :(得分:0)
尝试不使用NgZone
当您说“ ngOnInit()(ClosureFormComponent)没有被触发”时,您的意思是如果将console.log()放入构造函数中,则会被触发?