windows 重新加载后无法重定向路由

Not able to redirect the route after windows reload

我有两个页面,分别是角色列表和角色详细信息。根据角色的要求,我需要在单击保存按钮后隐藏菜单选项。我可以用下面的代码

windows.location.reload();

重新加载后,我需要导航到不同的页面。为此,我使用以下代码。

if(this.menuHide == "true"){     
   window.location.reload();
   this.router.navigateByUrl('/', {skipLocationChange: true}).then(()=>
   this.router.navigate(['role-list']));
} 

也尝试了这些选项

this.router.navigate(['/role-list']));
this.router.navigateByUrl('/role-list'); 

重新加载方法下面的代码没有执行,这意味着它没有重定向到 role-list 页面。我正在使用 angular 7 版本。

任何人都可以就如何导航到其他页面提出任何想法,我将不胜感激,在此先感谢。

你不应该打电话

window.location.reload();

您在这里所做的是重新加载整个页面并销毁 Angular 应用程序。你永远不应该在单页应用程序中这样做,例如使用 Angular 创建的应用程序。调用 reload 之后的任何代码将永远不会执行,因为您已经销毁了整个 JavaScript 应用程序并从头开始重新加载整个应用程序,这是不可取的。

在 Angular 中,我们使用一种现代编程风格,称为基于 RxJs observables 的反应式编程。这是一个远远超出 Whosebug 答案范围的概念,但您的菜单应该对角色的 Observable 做出反应。

如果你只想隐藏菜单你不需要重新加载整个页面,只需实现一个隐藏功能并使用它。 只有使用 "router" 才能在新选项卡中打开,您必须单独创建 link 并使用 "window.open()" 函数调用它。

可能会帮助您进行路由选择

我已经使用下面的代码解决了这个问题

  if(this.menuHide == "true"){
      this.router.navigate(['/role-list']).then(()=>{
           window.location.reload();
       });
    } 

它工作得很好..谢谢大家的宝贵建议。