Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change url without redirect in Angular 2

In my Angular 2 web application I would like to change the url page in the browser. For example I'd like to change it from:

http://localhost:63000/my-page

to

http://localhost:63000/your-page

It must not redirect, no page load, and browser history must reflect the page change.

Can this be done?

like image 289
brinch Avatar asked Mar 24 '17 21:03

brinch


1 Answers

This can be achieved by below function available in angular 4

location.replaceState(path: string, query?: string): void;

Before doing this you need to get location object by injecting in your component or directive constructor.

  constructor(private location: Location)

And importing Location class like this -

import { Location } from '@angular/common';
like image 116
Aamol Avatar answered Sep 21 '22 20:09

Aamol