【发布时间】:2017-10-07 00:15:12
【问题描述】:
我有两个具有父子关系的组件。父组件的 ngOnInit 方法检查用户是否登录,如果未登录则导航到登录页面。
class ParentComponent implements OnInit{
ngOnInit(){
if(!authService.loggedIn){
//navigate to login screen code
return;
}
}
}
class ChildComponent implements OnInit{
ngOnInit(){
/** POINT TO BE NOTED **/
// this function is also called even if ngOnInit
// of parent navigates to different component and returns.
// do some stuff with userService.token
// but because token isn't available the calls fail
}
}
如果父组件想要导航到其他组件,我如何阻止这个级联 OnInit 调用?
【问题讨论】:
标签: angular angular2-routing angular2-template ngoninit