(Angular 8)为什么无法重新加载强制重定向路线引用的路线?

问题描述 投票:0回答:1

我有一个选择元素的系统。每次更改此元素时,我都会刷新主组件。除主页外,其他任何地方都可以。

代码示例。这是我的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
    }
  },

当我使用根URL时,我在仪表板页面上被重定向:没关系。在每个页面上都可以从组件刷新页面。当我在仪表板页面上并且想要从同一组件重新加载页面时,它不起作用。

如果我对此部分发表评论:

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

...有效。

有人知道为什么不能同时做这两个...?我不明白为什么...

非常感谢您的帮助!

angular redirect root router reload
1个回答
0
投票

我不明白为什么要解决我原来的问题,但我想了解,但是我找到了解决我问题的方法。在相关组件(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();
    }
  }

我的重定向与以前相同,并且重新加载可在我的仪表板页面上进行。

我为我的情况调整了代码,但在这里找到了答案:[How to reload the current route with the angular 2 router。 (来自红豌豆的回复)。

© www.soinside.com 2019 - 2024. All rights reserved.