Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular router navigate then reload

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

like image 908
Omar Abdelhady Avatar asked Dec 01 '18 10:12

Omar Abdelhady


People also ask

Does router navigate refresh page?

By default, the router will ignore this navigation. However, this prevents features such as a “refresh” button.

What is the difference between navigate and navigateByURL in angular?

difference between the two navigateByUrl is similar to changing the location bar directly–we are providing the “whole” new URL. Whereas router. navigate creates a new URL by applying an array of passed-in commands, a patch, to the current URL.


3 Answers

Simple

this.router.navigate(['path/to'])
  .then(() => {
    window.location.reload();
  });
like image 117
Vlad Avatar answered Oct 02 '22 11:10

Vlad


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();
  }
});
like image 40
danielreiser Avatar answered Oct 02 '22 13:10

danielreiser


Simply do

location.href = '/admin/dashboard';
like image 6
Priesten Avatar answered Oct 02 '22 13:10

Priesten