Raph
Raph

Reputation: 521

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 ?

Upvotes: 6

Views: 35023

Answers (5)

Jainy
Jainy

Reputation: 11

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"]);
}

Upvotes: 1

Raph
Raph

Reputation: 521

I have finally found ! It's working with :

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

Thank you for helping me

Upvotes: 5

Thierry Templier
Thierry Templier

Reputation: 202306

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

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

Upvotes: 0

Michael Desigaud
Michael Desigaud

Reputation: 2135

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

Upvotes: 4

intuix
intuix

Reputation: 268

What is the error message ?

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

Upvotes: 0

Related Questions