javascript - Angular 4: Redirecting user to last visited page in previous session on login
问题描述
I have a task of redirecting an user to the last visited page after s/he logs in. Now I clearly have to store the router Url of the last visited page in my backend. My question is how can I do that so that I do not have to make a lot of backend calls? I am using Angular 4 + Spring boot. Please help me with suggestions and ideas.
解决方案
Save the last navigation status in local storage, which you can refer to later. For an example, you add something like following to your app component.
export class AppComponent {
constructor(private router: Router) {
const lastVisitedUrl: string = localStorage.getItem('last_visited_url');
localStorage.removeItem('last_visited_url');
if (lastVisitedUrl) {
this.router.navigateByUrl(lastVisitedUrl);
}
router.events.subscribe((event: RouterEvent) => {
if (event instanceof NavigationEnd) {
localStorage.setItem('last_visited_url', event.url);
}
});
}
}
推荐阅读
- apache-kafka - 获取特定项目的生产和消费时间戳的选项?
- python-3.x - 如何用多个数据框列的值替换列中的值
- python - 仅使用 def 返回文件的平均值
- python - append方法不正确?
- sql - 动态列别名创建和加入
- java - 函数式接口和递归
- javascript - 警告:失败的道具类型:提供给“路线”的无效道具“组件” - react-router-dom
- r - 如何从 tibble 中删除非缺失值与其他行中的值子集匹配的行?
- java - 如何在 java 中为我的 morse cod 编写 junit 测试
- ios - 不兼容的指针类型将“AVCapturePhotoSettings *”发送到“NSArray”类型的参数
* _Nonnull'