当用户访问 Ionic 4 中的特定路线时重定向用户
Redirect the user when the user visits the particular route in Ionic 4
我在我的 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 存储在存储中,因为我想多次使用它。
非常感谢任何建议或帮助。请帮我处理代码,因为我正在做一个项目,我想按时完成。
非常感谢任何帮助。
您可以使用守卫来完成此操作。守卫将确定用户是否登录。如果没有,用户将被重定向到另一条路线(登录页面或您希望他们登陆的任何地方)。
authentication.guard.ts
@Injectable({
providedIn: 'root'
})
export class AuthenticationGuard implements CanActivate {
constructor(private _router: Router) {}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
let isLoggedIn: boolean = false;
// NOTE: Do your logic here to determine if the user is logged in or not.
// return true if use is authenticated
if(isLoggedIn) return true;
// else redirect the user to another route and return false.
this._router.navigate(['login']);
return false;
}
}
tabs.router.module.ts
const routes: Routes = [
{
path: 'tabs',
component: TabsPage,
children: [
...
{
path: 'tab2',
canActivate: [AuthenticationGuard],
children: [
{
path: '',
loadChildren: '../tab2/tab2.module#Tab2PageModule'
}
]
},
...
]
}
...
];
Angular 守卫就像过滤器一样使用。您可以将 guards/filters 的数组添加到您的路线,必须满足所有这些才能访问该路线(充当链)。在您的路由数组中,将 canActivate
属性添加到要过滤的路由。在上面的示例中,我将 AuthenticationGuard
添加到 tab2
路由,当用户尝试访问 tab2
或其任何子级时,该路由只会 运行 。您可以将 canActivate
放在路由 (tabs
) 的根部以过滤 tabs
路由的所有子路由(将过滤 tab1
、tab2
等)。
我在我的 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 存储在存储中,因为我想多次使用它。
非常感谢任何建议或帮助。请帮我处理代码,因为我正在做一个项目,我想按时完成。
非常感谢任何帮助。
您可以使用守卫来完成此操作。守卫将确定用户是否登录。如果没有,用户将被重定向到另一条路线(登录页面或您希望他们登陆的任何地方)。
authentication.guard.ts
@Injectable({
providedIn: 'root'
})
export class AuthenticationGuard implements CanActivate {
constructor(private _router: Router) {}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
let isLoggedIn: boolean = false;
// NOTE: Do your logic here to determine if the user is logged in or not.
// return true if use is authenticated
if(isLoggedIn) return true;
// else redirect the user to another route and return false.
this._router.navigate(['login']);
return false;
}
}
tabs.router.module.ts
const routes: Routes = [
{
path: 'tabs',
component: TabsPage,
children: [
...
{
path: 'tab2',
canActivate: [AuthenticationGuard],
children: [
{
path: '',
loadChildren: '../tab2/tab2.module#Tab2PageModule'
}
]
},
...
]
}
...
];
Angular 守卫就像过滤器一样使用。您可以将 guards/filters 的数组添加到您的路线,必须满足所有这些才能访问该路线(充当链)。在您的路由数组中,将 canActivate
属性添加到要过滤的路由。在上面的示例中,我将 AuthenticationGuard
添加到 tab2
路由,当用户尝试访问 tab2
或其任何子级时,该路由只会 运行 。您可以将 canActivate
放在路由 (tabs
) 的根部以过滤 tabs
路由的所有子路由(将过滤 tab1
、tab2
等)。