简体   繁体   中英

Angular 2 router navigate function not working

I have a problem with the router function "navigate", in my AppComponent I have :

@RouteConfig([ 
  {path:'/home', name: 'Home', component: HomeComponent,  useAsDefault: true, data: {user: null}},
  {path:'/dashboard', name: 'Dashboard', component: DashboardComponent}
])

In my HomeComponent, I am trying to do this :

...
constructor(private _router:Router){}

changePage(){
  this._router.parent.navigate(["Dashboard"]); // It fails
}
...

It doesn't send me at '/dashboard', is this normal ?

I have finally found ! It's working with :

changePage() {
  this._router.navigate(["../Dashboard"]);
}

Thank you for helping me

why using parent ? it should be this._router.navigate(["Dashboard"]);

router.navigate is a method which take path as a parameter and navigate to that particular path and load component, I hope it will work.

constructor(private _router:Router){}
changePage(){
this._router.navigate(["dashboard"]);
}

What is the error message ?

Also why using this._router.parent.navigate rather than simply this._router.navigate.. ?

You could try the following to use a route defined in the AppComponent component:

changePage() {
  this._router.navigate(["/Dashboard"]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM