在我的 Angular 2 应用程序中,如果设置了 cookie,我希望重定向到某个页面。行为应该是:
- 没有 cookie -> 正常路由
- Cookie 已设置 -> 重定向到在 cookie 中设置的已保存路由
这样做的最佳做法是什么?我的想法是在根组件 AppComponent 中使用 ngOnInit(),然后检查是否设置了 cookie,如果当前 url 与 cookie 不同,则可能重定向。
这是一个可能的解决方案吗?
我的第一个问题是this.router.url
在 AppComponent 中总是“/”。
在 AppComponent 中的 ngOnInit() 中导航也不起作用:this.router.navigate(['/stepX']);