this.router.routeReuseStrategy.shouldReuseRoute = () => false;

Aza*_*han 2 javascript routes angular

this.router.routeReuseStrategy.shouldReuseRoute = () => false;
Run Code Online (Sandbox Code Playgroud)

我应用了这种行,以便每次都更新组件 UI。但在其他一些情况下,如果它应该重用路由,它就会开始刷新页面事件。

我们怎样才能克服这个问题呢?

实际上,在我的应用程序中,左侧面板中有三个选项卡。在每个选项卡中都有一些列表,单击列表项可打开右侧面板上的内容。但在其中一个列表中,有一个通用 UI 在某些列表项上打开,但问题是,当我们不应用上述代码时,UI 不会更新。但是,如果我们应用该代码,那么每次单击其他列表项时,UI 都会更新。但问题是,当我们应用此代码时,每次我们单击不同选项卡中的其他列表时,它都会开始刷新页面,但情况不应该如此。

如果我们应用此代码this.router.routeReuseStrategy.shouldReuseRoute = () => false;,那么我们如何在 this.router 下恢复此功能?

Sha*_*bob 6

To take less risks I'm just reverting it back to what it was once the reload is done:

refresh() {
  const prev = this.router.routeReuseStrategy.shouldReuseRoute;
  const prevOSN = this.router.onSameUrlNavigation;
  this.router.routeReuseStrategy.shouldReuseRoute = () => false;
  this.router.onSameUrlNavigation = 'reload';

  this.router.navigate([this.router.url])).then(() => {
    this.router.routeReuseStrategy.shouldReuseRoute = prev;
    this.router.onSameUrlNavigation = prevOSN;
  });
}
Run Code Online (Sandbox Code Playgroud)

  • 使用承诺恢复到以前的值可能会更好。this.router.navigate([this.router.url]).then(() => { this.router.routeReuseStrategy.shouldReuseRoute = prev; this.router.onSameUrlNavigation = prevOSN; }); (2认同)