0

I know I can do this

this.router.navigate(["/user-reviews"], username, userId, {skipLocationChange: true})

and the url won't change if I'm coming from

http://localhost:4200/main

and using skipLocationChange, but what I want it to say is http://localhost:4200/user-reviews/bob

How do I do it so that it only hides the userId parameter, but not the username and still retains the new path user-reviews instead of main? Is this possible? I appreciate any help!

1
  • you could probably do something to accomplish this, like changing the url to to reviews/bob and then again with the id and skip locaton change, but... this probably isn't the track you want to go down. what about in the case of a page refresh? angular won't have track of what happened or why. skipLocationChange is a useful tool for cases where you want to take advantage of the router but want to be in the same place as when you started, not for obscuring parts of the url.. Maybe put more into the question about why you want to accomplish this and you can get a differnt route Commented Apr 25, 2020 at 18:42

1 Answer 1

1

based on this post

you could easily do something like

ngOnInit()
{    
  this.location.replaceState("/user-reviews/something");
}

using location after navigate inside your component should do the trick.

Sign up to request clarification or add additional context in comments.

11 Comments

One issue though. If I refresh the page, then it resets the URL to just http://localhost:4200 with nothing after it so it doesn't stay in the same route.
Maybe you should put skiplocationchange to false
If I set it to false, then the userId shows up, which I don't want
@user6680 there must be something but usually (and I didn't tested it) but replaceState is supposed to replace the whole URL maybe you are having a problem with ngOnInit not triggerring properly. could you do a stackblitz to reproduce the error please
The problem is that I'm using the id for a GET request on ngOnInit, but when you replaceState it removes that id after refresh. For example localhost:4200/main/username/userId to localhost:4200/main/username. I refresh the page, but now I don't have the userId to do GET. I feel like I'm asking for an impossible thing lol, but I've considered localStorage. I'm just mostly concerned around the security aspect of exposing one's userId
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.