49

So I want to reload the app after navigating to a specific route ..

I use router.navigate to navigate to certain route based on user role and that works fine but I have to reload the page after routing if coming from sign in page (not every time user opens that certain route) - and reloading here is to update page language depending on user's language

so what i tried to do is

  else if(this.sp.role === 'Admin') {
      console.log('ooo')
      this.router.navigate(['/admin/dashboard']); 
      this.window.location.reload();

but that reloads the sign in page

1

4 Answers 4

154

Simple

this.router.navigate(['path/to'])
  .then(() => {
    window.location.reload();
  });
Sign up to request clarification or add additional context in comments.

6 Comments

its take too much time to reload page
@amitsutar sometimes you require to reload the page. This answer is for those circumstances
the answer I have been searching for for a long time, simple and effective
If that is to slow, do a real reload: window.location.href = window.location.protocol + '//' + window.location.host + '/path/to';. This is more in line with user expectations.
problem: you can see the navigated page flashing and then the reload fires. Not nice.
|
9

What you could do is shift the reload logic to the actual component which needs to be reloaded. You can subscribe to the NavigationEnd event and then check the route you're coming from e.g.

this.router.events
  .pipe(
    filter(value => value instanceof NavigationEnd),
  )
  .subscribe(event => {
  if (event.url === 'http://mypreviousUrl.com') {
    this.window.location.reload();
  }
});

Comments

9

Simply do

location.href = '/admin/dashboard';

1 Comment

It's perfect for my use case no navigating to route and refresh resulting in flash screen
8

i put this in my helperService:

reloadCurrentRoute() {
    let currentUrl = this.router.url;
    this.router.navigateByUrl('/', {skipLocationChange: true}).then(() => {
        this.router.navigate([currentUrl]);
    });
  }

then this in the file that contains the navigating logic:


_this.router.navigate(['somecomponent/anothercomponent']).then(() => {
    _this.helperService.reloadCurrentRoute();
});

All it does is store current url then navigates to the application root and back to current url forcing a reload.

Note that _this might be just this in your case depending on where and how it's nested in arrow functions. Window.location.reload() did not work for me, it ended up as an unauthorized route forcing me back to the application root route.

Comments

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.