angular8 - 浏览器后退按钮以角度 8 重定向到主页
问题描述
浏览器后退按钮不起作用。它会将我重定向到主页,而不是转到上一页。例如 http://localhost:4200/#/component1/1 http://localhost:4200/#/component2/1 http://localhost:4200/#/component3/1
当我在组件 3 中单击后退按钮时,它会转到组件 2,而不是转到主页。谁能告诉我这个问题的解决方案?
路由.ts
{
path: 'component1/:id',
component: Component1,
canActivate: [OAuthCallbackHandlerGuard],
data: { isId: true },
},
{
path: 'component2/:id',
component: Component2,
canActivate: [OAuthCallbackHandlerGuard],
data: { isId: true },
},
{
path: 'component3/:id',
component: Component3,
canActivate: [OAuthCallbackHandlerGuard],
data: { isId: true },
},
oauthcallbackhandlerguard.service.ts
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | UrlTree> {
this.adalService.handleWindowCallback();
let engId: number = 0;
let engagementDetailId: number = 0;
if(route.data.isEngagementDetailId == true)
{
let engDetailData =this.engagementService.$engagementDetailId.subscribe((data) => {
if (data !== undefined && data !== null) {
engagementDetailId = data;
}
});
}
else
{
let engData =this.engagementService.$engagementId.subscribe((data) => {
if (data !== undefined && data !== null) {
engId = data;
}
});
}
let groupIds = JSON.parse(sessionStorage.getItem('groupids'));
return this.validateUserAccess(engId, engagementDetailId, state,groupIds);
}
private validateUserAccess(engId: number, engagementDetailId: number, state: RouterStateSnapshot,groupIds:number[]): Observable<boolean | UrlTree> {
if (engId > 0 || engagementDetailId>0) {
return this.authGuardService.validateUserAccess(engagementDetailId, engId,groupIds).map(response => {
console.log(response);
return response ? true : this.router.parseUrl('/unauthorized');
if (response) {
this.router.navigate([state.url]);
}
else {
this.redictToUnAutherized();
}
},
(error) => {
console.log('auth failed. Error ', error);
// this.redictToUnAutherized();
return false;
});
}
else {
return Observable.of(true);
}
}
private redictToUnAutherized() {
this.router.navigate(['/unauthorized']);
}
解决方案
1-在不同的浏览器中运行您的应用程序
2-添加默认路由
const routes: Routes = [
{ path: '', pathMatch: 'full', component: HomeComponent },
{
path: 'component1/:id',
component: Component1,
canActivate: [OAuthCallbackHandlerGuard],
data: { isId: true },
},
{
path: 'component2/:id',
component: Component2,
canActivate: [OAuthCallbackHandlerGuard],
data: { isId: true },
},
{
path: 'component3/:id',
component: Component3,
canActivate: [OAuthCallbackHandlerGuard],
data: { isId: true },
},
{ path: '**', component: HomeComponent }
];
3-尝试检查 OAuthCallbackHandlerGuard 是否要导航页面并使用 Auth 允许它
constructor (private _location: Location)
this._location.back();
推荐阅读
- jupyter - Jupyterlab 不显示一些在 jupyter notebook 中可见的单元格
- java - 是否可以从具有限制的 Json 对象中获取值
- java - 所有正则表达式都需要量词吗?
- docker - 如何在负载均衡器后面获取 kong 集群的指标
- linux - 在 Mac/Linux 上使用特定规则重命名文件和文件夹的递归 bash 脚本
- angular - 当 api 返回 404 时,Observable completed 没有被调用,当 api 失败时有任何替代解决方案吗?
- python - 我正在尝试在 colab 中使用 tkinter,问题就像基本的标记表生成一样我收到了这个错误
- regex - 具有反向引用的相同发送者和接收者的 Bash 正则表达式
- docker - 从 cloud-builders/docker 步骤中登录私有注册表失败 - 证书由未知机构签名
- flutter - 为什么不能在颤振的主要中使用await getTemporaryDirectory