angular - Angular:在不同条件下登录后重定向到不同页面
问题描述
我有一个简单的登录,您可以在其中输入电子邮件和密码,如果它在数据库中可用,则用户可以登录。但登录后的用户必须重定向到特定页面。
例如:如果用户登录类型是“供应商”,则用户必须重定向到特定页面,否则用户必须重定向到另一个页面。
这是服务方法
login(email, password): any {
const headers = new HttpHeaders({
'Content-Type': 'application/json'
});
return this.http
.post(this.uri + '/login', { email, password }, {headers})
.pipe(catchError(this.errorHandler));
}
如果用户没有输入“供应商”,我想重定向到仪表板
this.router.navigate(['dashboard']);
如果用户是供应商,我想重定向到“供应商”
this.router.navigate(['supplier']);
来自后端的用户对象如下
error: false
result:
email: "din@gmail.com"
phone_no: "00000008"
type: "supplier"
user_id: 1
对于 ref,这是 ts 文件方法
loginToSystem(): any {
const email = this.formData().email;
const password = this.formData().password;
this.backendService.login(email, password)
.subscribe(data => {
this.loginData = data;
console.log(data);
// console.log('helo');
console.log(data.result.type);
});
console.log(email, password);
}
应用程序路由
const routes: Routes = [
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
{path: 'login', component: LoginComponent},
{path: 'dashboard', component: DashboardComponent},
{path: 'supplier', component: SupplierComponent}
];
感谢您对此事的任何见解!谢谢!
解决方案
一个简单的 if() 不应该解决您的问题吗?
loginToSystem(): any {
const email = this.formData().email;
const password = this.formData().password;
this.backendService.login(email, password)
.subscribe(data => {
this.loginData = data;
if(data.result.type === 'supplier') {
this.router.navigate(['supplier']);
} else {
this.router.navigate(['dashboard']
}
});
console.log(email, password);
}
如果您只有 2 个 level_types,这将是一个很好的解决方案
推荐阅读
- macos - 如何将 Rust 应用程序从 macOS x86 交叉编译到 macOS Silicon?
- javascript - 当我使用 PrivateRoute 时,我总是在重新加载页面时进入一次登录页面
- angularjs - 使用 stateProvider 进行路由时,如何在 AngularJS SPA 上获取 URL 参数
- node.js - npm serial-port 获取输出以响应组件
- python - 设置蟒蛇
- spring-boot - Spring Boot SecurityContextHolder.getContext().getAuthentication() 在基本身份验证上返回 null
- apache - 如何将 HTACCESS 更改重定向到现在允许子目录 URL?
- javascript - 在 mongodb 中存储/更新深度嵌套的对象数组
- elasticsearch - 为什么不同设备的最大指标结果相同?
- elasticsearch - ElasticSearch - 嵌套数组中的唯一计数