0

I have a system where I select an element. I would to refresh my main component each time I change this element. It's okay everywhere except on the main page.

Example by the code. It's the concerned part in my app-routing.module.ts :

  /******* Default route *******/
  {
   path: '',
   redirectTo: '/' + Route.DASHBOARD,
   pathMatch: 'full'
  },

  /******* DASHBOARD *******/
  {
    path: Route.DASHBOARD, component: DashboardComponent,
    canActivate: [AuthGuard],
    data: {
      title: i18nlib.TITLE.DASHBOARD,
      icon: constant.ICON.DASHBOARD,
      breadcrumb: i18nlib.TITLE.DASHBOARD
    }
  },

When I'm on the root URL, I'm redirected on the dashboard page : it's okay. The refresh of the page from the component is okay on each page. When I'm on the dashboard page and I want to reload my page from the same component, it doesn't work.

If I comment this part :

  /******* Default route *******/
  {
   path: '',
   redirectTo: '/' + Route.DASHBOARD,
   pathMatch: 'full'
  },

... it works.

Someone knows why it's not possible to do the both at the same time... ? I don't understand why...

Thank you very much for helping !

Jiizen
  • 119
  • 1
  • 11

1 Answers1

0

I don't understand why for my original problem and I would to understand, but I found something to resolve my problem. In the concerned component (DashboardComponent) :

navigationSubscription;

  constructor(
    private router: Router,
  ) {
    // subscribe to the router events. Store the subscription so we can
    // unsubscribe later.
    this.navigationSubscription = this.router.events.subscribe((e: any) => {
      // If it is a NavigationEnd event re-initalise the component
      if (e instanceof NavigationEnd) {
        this.ngOnInit();
      }
    });
  }


  ngOnDestroy() {
    if (this.navigationSubscription) {
      this.navigationSubscription.unsubscribe();
    }
  }

My redirect is the same than before and my reload works on my dashboard page.

I adjusted the code for my case, but I found the answer here : [How to reload the current route with the angular 2 router. (Response from The Red Pea).

Jiizen
  • 119
  • 1
  • 11