【发布时间】:2020-07-29 12:49:51
【问题描述】:
当我使用 CTRL + R 或 F5 重新加载页面或打开新选项卡时,总是以 8 角重定向到主页。
我的路线设置在这里
const routes: Routes = [
{ path: 'dashboard', component: OrderComponent, canActivate: [AuthGuard] },
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{ path: 'login', component: LoginComponent },
{ path: 'orders', component: OrderComponent, canActivate: [AuthGuard] },
{ path: 'add-product', component: AddProductComponent, canActivate: [AuthGuard] },
{ path: 'past-orders', component: PastOrdersComponent, canActivate: [AuthGuard] },
{ path: 'today-orders', component: TodayOrdersComponent, canActivate: [AuthGuard] },
{ path: 'schedule-orders', component: ScheduleOrdersComponent, canActivate: [AuthGuard] },
{ path: 'products', component: ProductsComponent, canActivate: [AuthGuard] },
{ path: 'edit-product/:product_id', component: AddProductComponent, canActivate: [AuthGuard] },
{ path: 'invoice/:order_id', component: InvoiceComponent },
{ path: 'accept-order/:order_id', component: SingleOrderComponent, canActivate: [AuthGuard] },
{ path: 'timing', component: TimingComponent, canActivate: [AuthGuard] },
{ path: 'settings', component: SettingsComponent, canActivate: [AuthGuard] },
{ path: '**', component: PageNotFoundComponent },
];
@NgModule({
// { useHash: true }
imports: [RouterModule.forRoot(routes, { onSameUrlNavigation: 'reload', enableTracing: false, useHash: true })],
exports: [RouterModule]
})
export class AppRoutingModule { }
我的功能是打开一个不应重定向到主页的新标签
const url = this.router.serializeUrl(
this.router.createUrlTree(['/invoice', '8088299'])
);
window.open(url, '_blank');
auth.guard.ts
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | UrlTree {
if (!this.authService.isLoggedin()) {
this.router.navigate(['login']);
});
return false;
}
return true;
}
}
this.authService.isLoggedin()下面的函数代码不是HTTP请求
isLoggedin() {
// `!!` returns boolean
return !!localStorage.getItem('token');
}
【问题讨论】:
-
能否也添加 AuthGuard 的代码。
-
没有启用AuthGuard的路由会不会出现这个问题?请分享 AuthGuard 代码以便更好地理解?
-
如何为您的应用程序提供服务?
-
@RahulSingh 添加
-
你能分享你的 authguard
标签: javascript angular angular8