Replace the current route with other route

In Angular 2.0 to navigate to other route we can use route.navigate( [ 'routeName' ] ). But this action will store the current route in browser history.
What is the method to replace the current route in browser history and navigate to new route.

3
Leave a Reply

avatar
3 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

You can use

navigateByUrl(url: string, _skipLocationChange?: boolean) : Promise<any>

or

navigateByInstruction(instruction: Instruction, _skipLocationChange?: boolean) : Promise<any>

on the Router and pass true to _skipLocationChange to achieve that effect.

(not tried myself yet though and the docs are vague but I think this is what you’re looking for)

Hint: not yet supported in the new RC.1 router

Jason
Guest

See this: https://angular.io/docs/ts/latest/api/router/index/NavigationExtras-interface.html

You have to use the option replaceUrl instead of skipLocationChange. It will send you to the new url but the current one will NOT be preserved, so pressing back will not send you back to this page.

this.router.navigate(['login'],{replaceUrl:true});

Jason
Guest

You can try this, it works on Angular RC.1 + versions

this.router.navigate(['login'],{skipLocationChange:true});

This will navigate to login route and not store login route in Browser History.