Angular 2 - 我正在尝试使用router.parent.navigate导航到另一个路由 [英] Angular 2 - I'm trying to navigate to another route using router.parent.navigate
本文介绍了Angular 2 - 我正在尝试使用router.parent.navigate导航到另一个路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Angular 2 - 我正在尝试使用router.parent.navigate导航到另一个路由。
Angular 2 - I'm trying to navigate to another route using router.parent.navigate.
但是,我得到的错误是:EXCEPTION:TypeError :无法在我的控制台日志中读取属性导航null。
However, the error I get is this: "EXCEPTION: TypeError: Cannot read property 'navigate' of null" in my console log.
这是我当前的代码:
import {Component, View, bootstrap, bind, provide} from 'angular2/angular2';
import {Router, ROUTER_BINDINGS, RouterOutlet, RouteConfig, RouterLink, ROUTER_PROVIDERS, APP_BASE_HREF} from 'angular2/router';
import {Location, LocationStrategy, HashLocationStrategy} from 'angular2/router';
import {Todo} from './components/todo/todo';
import {About} from './components/about/about';
import {AuthService} from './authService';
@Component({
selector: 'app'
})
@View({
template: `
<div class="container">
<nav>
<ul>
<li><a [router-link]="['/Home']">Todo</a></li>
<li><a [router-link]="['/About']">About</a></li>
</ul>
</nav>
<router-outlet></router-outlet>
</div>
`,
directives: [RouterOutlet, RouterLink]
})
@RouteConfig([
{ path: '/', redirectTo: '/home' },
{ path: '/home', component: Todo, as: 'Home' },
{ path: '/about', component: About, as: 'About' }
])
export class AppComponent {
constructor(_router: Router, _authService: AuthService, _location: Location){
_router.subscribe((val) => {
_authService.isUserLoggedIn().then((success) => {
//This part below is not working:
_router.parent.navigate(['/About']);
});
})
}
}
bootstrap(AppComponent, [ROUTER_PROVIDERS, provide(APP_BASE_HREF, {useValue: '/'}), AuthService]);
推荐答案
我认为你应该使用ngOnInit方法。
I think you should use the ngOnInit method.
export class AppComponent implements OnInit {
constructor(router: Router, authService: AuthService, location: Location){
}
ngOnInit(){
this.router.subscribe((val) => {
this.authService.isUserLoggedIn().then((success) => {
//This part below is not working:
this.router.parent.navigate(['/About']);
});
})
}
}
这篇关于Angular 2 - 我正在尝试使用router.parent.navigate导航到另一个路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文