【发布时间】:2025-10-09 09:10:01
【问题描述】:
我正在使用我的 Ionic 4 应用程序,我正在使用登录系统,当用户登录时,它将重定向到用户可以检查用户挑战的页面以及用户未登录时的页面,如果它尝试访问该页面,然后它应该重定向到另一个页面。
这是我的userlogin.ts:
async UserLoginDetails($soctype, $socid) {
const loading = await this.loadingController.create({
message: 'Please Wait',
duration: 1100,
translucent: true,
});
await loading.present();
const userdetailslogin = {
email: this.userlogindet.value.email,
password: this.userlogindet.value.password,
social_type: $soctype,
social_id: $socid,
};
this.chakapi.loginUser(userdetailslogin, 'userLogin').subscribe((data) => {
console.log(data);
if (data) {
this.responseEdit = data;
if (this.responseEdit.status === 'success') {
console.log(this.responseEdit.data.id);
this.storage.set('ID', this.responseEdit.data.id);
this.presentAlertConfirm('Login Successful', 1);
} else {
this.presentAlertConfirm('Either You are not registered Or not approved user.', 0);
}
}
});
return await loading.onDidDismiss();
}
async presentAlertConfirm($messge, $para) {
const alert = await this.alertController.create({
message: $messge,
buttons: [
{
text: 'Cancel',
role: 'cancel',
cssClass: 'secondary',
handler: () => {
// console.log('Confirm Cancel: blah');
if ($para === 1) {
this.modalController.dismiss();
this.router.navigate(['/tabs/tab2']);
}
}
}]
});
await alert.present();
}
当用户登录时,其用户 ID 将存储在存储中。
这是我的tabs.router.module.ts:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { TabsPage } from './tabs.page';
const routes: Routes = [
{
path: 'tabs',
component: TabsPage,
children: [
{
path: 'tab1',
children: [
{
path: '',
loadChildren: '../tab1/tab1.module#Tab1PageModule'
}
]
},
{
path: 'tab2',
children: [
{
path: '',
loadChildren: '../tab2/tab2.module#Tab2PageModule'
}
]
},
{
path: 'tab4',
children: [
{
path: '',
loadChildren: '../login/login.module#LoginPageModule'
}
]
},
{
path: 'tab3',
children: [
{
path: '',
loadChildren: '../tab3/tab3.module#Tab3PageModule'
}
]
},
{
path: '',
redirectTo: '/tabs/tab1',
pathMatch: 'full'
}
]
},
{
path: '',
redirectTo: '/tabs/tab1',
pathMatch: 'full'
}
];
@NgModule({
imports: [
RouterModule.forChild(routes)
],
exports: [RouterModule]
})
export class TabsPageRoutingModule {}
我希望当用户未登录并尝试访问tab2 路由时,它应该重定向到其他页面。
我应该使用警卫服务还是正确地执行此操作。我将用户 ID 存储在存储中,因为我想多次使用它。
非常感谢任何建议或帮助。请帮我写代码,因为我正在做一个项目,我想按时完成它。
非常感谢任何帮助。
【问题讨论】:
标签: angular ionic-framework routing ionic4