【发布时间】:2020-03-12 09:30:53
【问题描述】:
我的初始域是 localhost:4200,它根据我的路由将我重定向到某个页面,但我想输入一个无效域,例如 localhost:4200/Whasup 或 localhost:4200/Whasdown,其中 Whasup 和 whasdown页面不存在。在我被重定向到默认的 Angular 404 页面之前,我需要检查一些条件。如果我的条件不满足,那么我想将其重定向到 404 页面,否则我想处理它。
my app-routing.module.ts(Auth Guard 检查用户是否登录)
const routes: Routes = [
// Fallback when no prior route is matched
// Shell.childRoutes([]),
// { path:'home', component: HomeComponent},
// { path: '**', redirectTo: '', pathMatch: 'full' }
Shell.childRoutes([
{ path: '', component: RedirectToComponent, canActivate: [AuthGuard] },
{
path: 'dashboard',
loadChildren: 'src/app/dashboard/dashboard.module#DashboardModule',
canActivate: [AuthGuard]
},
{
path: 'exec-dashboard',
loadChildren:
'src/app/exec-dashboard/exec-dashboard.module#ExecDashboardModule',
canActivate: [AuthGuard]
},
{
path: '**',
redirectTo: '',
canActivate: [AuthGuard]
}
]),
{
path: 'auth',
loadChildren: 'src/app/auth/auth.module#AuthModule',
canActivate: [AuthGuard]
}
];
@NgModule({
imports: [
RouterModule.forRoot(routes, {
enableTracing: false, // Enable for debug purpose.
useHash: true,
preloadingStrategy: PreloadAllModules
})
],
exports: [RouterModule],
providers: [AuthGuard]
})
我还实现了 KeyCloak Angular 拦截器,如果用户未登录,它可以帮助我进入 keycloak 登录页面。我在 app.module.ts 中定义了提供程序。
providers: [
{
provide: APP_INITIALIZER,
useFactory: initializer,
multi: true,
deps: [KeycloakService]
}
],
所以每当我点击我的初始域(localhost:4200)时,初始化函数在我被重定向到任何页面之前都会被调用,同样的情况也会发生在无效的子域 url(localhost:4200/Whasup)上。 编辑:我可以实现上述任何机会,或者我可以在角度实现子域逻辑,例如,tenant1.localhost:4200 或tenant2.localhost:4200,(部署时它将是tenant1.mycompany.com)。有人可以建议我一种以角度实现子域路由的方法。 非常感谢。
【问题讨论】:
-
根据您的描述,如果用户登陆不存在的页面,您希望将用户重定向到登录页面。这个逻辑与是否有子域无关。您可以简单地重定向到路由配置中“**”的登录页面(stackoverflow.com/a/36261194)。我想这就是你所追求的。
-
@DanielGrima 如果你看到我的应用程序路由模块,我已经完成了那部分,实际上在我的情况下,当我启动我的应用程序时,它不应该重定向到 localhost:4200,它应该直接去到 localhost:4200/tenantname 或tenantName.localhost:4200。
-
我已经发布了一个答案,因为我的解释无法放入评论中。希望对您有所帮助。
标签: javascript html angular routing