user3603575
user3603575

Reputation: 99

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.

Upvotes: 4

Views: 4096

Answers (3)

fantasma_del_espacio
fantasma_del_espacio

Reputation: 71

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

Upvotes: 7

Parth Ghiya
Parth Ghiya

Reputation: 6949

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.

Upvotes: 0

Günter Zöchbauer
Günter Zöchbauer

Reputation: 658087

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

Upvotes: 0

Related Questions