【问题标题】:Angular 12: navigate and reload pageAngular 12:导航和重新加载页面
【发布时间】:2022-01-18 18:17:20
【问题描述】:

我有 Angular 应用程序,我需要在导航后重新加载页面。我已经应用了this 解决方案,但window.location.reload(); 不适用于生产。所以我在app-routing.module.ts 文件中添加了useHash: true。但这也给我在应用程序中应用 SSR 时带来了问题。所以我需要以下代码的替代方法:

this.router.navigate(['path/to'])
  .then(() => {
    window.location.reload();
  });

我尝试过应用window.location.href = window.location.hrefwindow.location.href = window.location.protocol + '//' + window.location.host + '/path/to'; 而不是window.location.reload();,但它没有按预期重新加载页面。

在 Angular 12 中导航后,还有其他方法可以实现重新加载吗?请帮助和指导。

编辑: 为什么要重新加载: 在应用程序中,用户登录后,通知显示在标题中。如果页面未重新加载,则不会显示数据。

【问题讨论】:

  • 为什么需要重新加载?
  • 看起来你想要一个用 JavaScript 编写的锚标记行为。我说的对吗?
  • 我认为你解决了错误的问题。如果您想刷新登录通知,为什么不使用订阅或状态解决方案(如 redux)从任何 placve 更新状态?
  • @MaartenDev 请检查有问题的编辑,以解决您的问题
  • @MaartenDev 我是角度的初学者,不知道您建议的解决方案。你能举一些你的角度解决方案的例子吗?请指导。

标签: javascript angular typescript navigation reload


【解决方案1】:

您可以尝试重定向到根目录,然后重定向到您的最终目的地:

this.router.navigate(['/'])
  .then(() => {
    this.router.navigate(['path/to'])
  });

【讨论】:

  • 上面的代码也试过了。不重新加载页面:(
  • 你试过没有useHash吗?
  • 删除了useHash 并尝试了上面的代码仍然没有重新加载。
猜你喜欢
  • 1970-01-01
  • 2014-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多