【问题标题】:(Angular 8) Why it's not possible to reload the route referenced by the force redirected route?(Angular 8)为什么无法重新加载强制重定向路由引用的路由?
【发布时间】:2020-06-29 21:05:14
【问题描述】:

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

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

    我不明白我原来的问题的原因,我想理解,但我找到了解决问题的方法。在相关组件(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. (来自红豌豆的回应)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      • 2018-12-27
      • 2015-10-03
      • 2021-01-28
      • 2021-12-16
      • 2017-05-12
      相关资源
      最近更新 更多