简体   繁体   English

如果用户处于注销状态,Angular2,重定向到登录页面

[英]Redirect to login page if user is in logout state, Angular2

I am developing a single page app using AngularJS2, I want to redirect to login page if user is not login, how can I check condition for each route to check for login? 我正在使用AngularJS2开发单页应用程序,如果用户未登录,我想重定向到登录页面,如何检查每条路由的条件以检查登录?

import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { LoginComponent } from './login/login.component';
import { NotFoundComponent } from './not-found/not-found.component';
import { SalesComponent } from './sales/sales.component';
import { LogoutComponent } from './logout/logout.component';

const appRoutes: Routes = [
  { path: 'home', component: HomeComponent },
  { path: 'login', component: LoginComponent },
  { path: 'employees', component: EmployeeComponent },
  { path: '404-not-found', component: NotFoundComponent },
  { path: 'logout', component : LogoutComponent },
  { path: '**', redirectTo: '/404-not-found' }
];

export const routing = RouterModule.forRoot(appRoutes);

I want to check for login status in app.routing.ts file, and if I am not login I should redirect to login. 我想检查app.routing.ts文件中的登录状态,如果我没有登录,我应该重定向到登录。 Thanks for help. 感谢帮助。

Please refer to the answers at Angular2 redirect to login page 请参阅Angular2重定向到登录页面的答案

The solution using CanActivate interface implementation is a robust solution with the current release of the new Router. 使用CanActivate接口实现的解决方案是新路由器当前版本的强大解决方案。

您必须按照路由和导航文档中的说明创建身份验证服务和身份验证服务,并且需要在路由配置中对其进行配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM