angular - 当用户重定向到登录时如何保留最后一条路线-by Ngrx Effect angular
问题描述
假设用户访问http://sample.com/dashboard然后登录保护检查并且他没有登录并将他重定向到登录页面。我想获得他的最后一条路线(在此示例中为http://sample.com/dashboard),并在他登录时将他重定向到该路线。我只是用 Ngrx Effect 做到的,但是......
@Effect()
getUserLoginData$: Observable<Action> = this.actions$.pipe(
ofType(userActions.GET_USER_DATA),
switchMap(() => {
return this.sessionService.getLoggedInUserInfo().pipe(
map((success) => new userActions.GetUserDataSuccessAction(success)),
catchError((error) => of(new uerActions.GetUserDataFailAction(error)))
);
})
);
@Effect({dispatch: false})
public userLoginDataSuccess$: Observable<Action> = this.actions$.pipe(
ofType(userActions.GET_USER_DATA_SUCCESS),
map((action: userActions.GetUserDataSuccessAction) => action),
tap((data) => {
if (data.url.includes('login'))
this.router.navigate([RouteMap.DASHBOARD])
else
this.router.navigate([data.url])
})
);
解决方案
例如,您可以创建一个服务来跟踪您的路线历史。
previousUrl: string;
constructor(router: Router) {
router.events
.filter(event => event instanceof NavigationEnd)
.subscribe(e => {
console.log('prev:', this.previousUrl);
this.previousUrl = e.url;
});
}
您可以将其与您的状态管理集成。
推荐阅读
- angular - 尝试使用 Angular 6 和 Spring Boot 登录时出现 404 错误
- python - Python SQLAlchemy+Threading 静默失败
- angular - Display fetched data from api
- c - Xcode 可执行包资源不适用于沙盒,无法写入“模块”文件;节流
- javascript - 在 Yii2 中包含 javascript 文件作为内联 javascript
- r - “闪亮的应用程序”上传文件 -> 做某事 -> 输出文件
- c# - TortoiseSVN 更新 svn:property 每次提交时都在同一个文件上
- android - java.text.ParseException:字符串无法解析的日期
- python - 启用 chrome 弹出窗口的 Python 脚本(下载前询问保存位置)?
- powershell - 用Powershell重命名文件,文件名已经存在