首页 > 解决方案 > Router RedirectTo 默认不触发事件 NavigationEnd

问题描述

我正在使用 Angular v6.1.0 和。我的路由文件在这里:

const routes: Routes = [
  {
    path: '', component: DashboardComponent, children: [
      { path: '', redirectTo: 'activity', pathMatch: 'full' },
      { path: 'activity', component: ActivityComponent },
      { path: 'recent-activity', component: RecentActivityComponent },
      { path: 'notification', component: NotificationComponent }
    ]
  },

];

这是我订阅导航事件的代码:

router.events.subscribe((event: Event) => {
      if (event instanceof NavigationStart) {
        // Do something here
      } else if (event instanceof NavigationEnd) {
        // Do something here
      }
    });

当我使用 router.navigation 时,会触发 NavigationEnd。
但是,如果我转到/dashboard路由器,它将重定向到上述路由文件中的/dashboard/activity,则不会触发该事件。

更新 这是我在StackBlitz
上的示例 只需输入没有任何路径的域,控制台仅在重定向到登录时记录“/”(我更喜欢记录“/登录”)

标签: angularangular6router

解决方案


推荐阅读