Scien Ce Subject
Scien Ce Subject

Reputation: 57

Angular2 back button issue | URL not changing | Angular Universal

Its kinda weird problem but I dunno what's going on. When I click the back browser button, the page changes but the URL remains the same. When I click the back button again, the URL changes but the page remains the same.

This was fixed after using

window.history.pushState({},'',url);

but with this... the problem is that I loop around 2 pages if I keep clicking back button.

EXAMPLE :-

A -> B -> C -> (back) -> B -> (back) -> C -> (back) -> B -> (back) -> C-> (back) -> B -> (back)

NOTE :- I have RouterModule.forRoot() in root routing module and RouterModule.forChild() in child routes.

I am using angular universal.

What could be the possible issue? How can this be solved?

----------------------------------- XXXXXX -----------------------------------------------

UPDATE

Was playing with code and i ran into Location of @angular/common. 
This is great with popstate (resolved the url change issue) but the problem now is...
whenever i hit back button...it pop same page twice....

EXAMPLE :-

A -> B -> C -> (back) -> B -> (back) -> B -> (back) -> A -> (back) -> A 

----------------------------------- XXXXXX -----------------------------------------------

UPDATE 2:-

when using location.. i checked the history length... 
the history length is not changing when the back button is clicked first time...

EXAMPLE :-

A : length = 1 -> B : length = 2 -> C : length = 3 -> (back) -> B : length = 3 -> (back) -> 
B : length = 2 -> (back) -> A : length = 2 -> (back) -> A : length = 1 

P.S. -> Check the link before asking for library versions.

Upvotes: 0

Views: 1996

Answers (1)

Himanshu Bansal
Himanshu Bansal

Reputation: 2093

Check the file

app-routing.module.ts

and make sure that the imported routes are like

RouterModule.forChild([])

rather than

RouterModule.forRoot([])

It will solve the issue.

Upvotes: 1

Related Questions