Javascript 角度2-I';我正在尝试使用router.parent.navigate导航到另一个路由

Javascript 角度2-I';我正在尝试使用router.parent.navigate导航到另一个路由,javascript,angular,Javascript,Angular,角度2-我正在尝试使用router.parent.navigate导航到另一条路线 但是,我得到的错误是:“EXCEPTION:TypeError:无法读取控制台日志中null的属性'navigate' 这是我当前的代码: import {Component, View, bootstrap, bind, provide} from 'angular2/angular2'; import {Router, ROUTER_BINDINGS, RouterOutlet, RouteConfig,

角度2-我正在尝试使用router.parent.navigate导航到另一条路线

但是,我得到的错误是:“EXCEPTION:TypeError:无法读取控制台日志中null的属性'navigate'

这是我当前的代码:

 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]);
从'angular2/angular2'导入{组件、视图、引导、绑定、提供};
从“angular2/Router”导入{Router,Router_绑定,RouterOutlet,RouteConfig,RouterLink,Router_提供者,APP_BASE_HREF};
从“angular2/router”导入{Location,LocationStrategy,HashLocationStrategy};
从“./components/Todo/Todo”导入{Todo};
从“./components/About/About”导入{About};
从“/AuthService”导入{AuthService};
@组成部分({
选择器:“应用程序”
})
@看法({
模板:`
  • 待办事项
  • 关于
`, 指令:[路由器输出,路由器链接] }) @线路图([ {路径:'/',重定向到:'/home'}, {path:'/home',组件:Todo,as:'home'}, {路径:'/about',组件:about,as:'about'} ]) 导出类AppComponent{ 构造函数(_路由器:路由器,_authService:authService,_位置:位置){ _路由器订阅((val)=>{ _authService.isUserLoggedIn()。然后((成功)=>{ //以下部分不起作用: _router.parent.navigate(['/About']); }); }) } } 引导(AppComponent,[ROUTER_PROVIDERS,provide(APP_BASE_HREF,{useValue:'/'}),AuthService]);
我认为你应该使用ngOnInit方法

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']);

        });      

    })
}
}

和我一样,它应该是
\u router.navigate(['/About'])<代码>路由器
。感谢您的帮助。嗯。。。是的,它是:_router.parent.navigate(['About']);只需删除正斜杠,使其相对于父对象而不是根对象