Angular 8 路由器在尝试激活同一路由时不会触发任何带有 `onSameUrlNavigation: 'reload'` 的事件

Angular 8 router does not fire any events with `onSameUrlNavigation: 'reload'` when trying to activate the same route

路由器配置:

imports: [
    RouterModule.forRoot(routes, {
      onSameUrlNavigation: 'reload',
      scrollPositionRestoration: 'top'
    })
  ],

我的路线:

  {
    path: 'orders',
    loadChildren: './modules/order/order.module#OrderModule',
    canLoad: [OrderGuard],
    runGuardsAndResolvers: 'always'
  },

然后在我的孩子中:

 {
        path: 'new-order',
        component: NewOrderComponent,
        pathMatch: 'full',
        canActivate: [OrderGuard],
        runGuardsAndResolvers: 'always'
      },

在我的实际组件中:

 this.routerSubscription = this.router.events.subscribe(event => {
      console.log('NAVIGATION EVENT', event);
      if (event instanceof NavigationEnd) {
        console.log('NAVIGATION END');
        this.ngOnDestroy();
        this.initializeComponent();
      }
    });

尝试 re-activate 相同路线后,不会触发任何事件。不过,事件确实会在初始激活时出现。

编辑:守卫确实会被触发。

编辑:我的 ngOnDestroy 代码搞砸了:

 if (this.routerSubscription) {
      this.routerSubscription.unsubscribe();
    }

不幸的是onSameUrlNavigation只执行守卫和解析器,它不会重新初始化组件。

一个很好的部分是它确实会触发导航,因此您有两种方法可以遵循

您可以使用 RouterModule 中的 Events 将其处理为

constructor(private _router: Router) {}

ngOnInit() {
  this._router.events.subscribe(res => {
    if (res instanceof NavigationEnd) console.log("NavigationEnd Event");
  });
}

您可以在路由器导航方法中传递一个随机数并订阅该参数来执行您的逻辑

Stackblitz 在 https://stackblitz.com/edit/angular-route-reload

编辑:

正如我在评论中指出的那样,似乎 ngOnDestroy 方法中发生了某些事情,并且您似乎在调用 ngOnDestroy 时取消了对路由事件订阅的订阅。 fiddle 已更新以显示此场景。

这不仅会执行一次路由事件。