我有一个auth
模块,其中包含几个组件:login
,signup
,logout
。
我在路由模块中声明所有路由:
import { NgModule } from "@angular/core";
import { Routes, RouterModule } from "@angular/router";
import { LoginComponent } from "./login/login.component";
import { SignupComponent } from "./signup/signup.component";
import { LogoutComponent } from "./logout/logout.component";
const routes: Routes = [
{
path: "auth/login",
component: LoginComponent
},
{
path: "auth/signup",
component: SignupComponent
},
{
path: "auth/logout",
component: LogoutComponent
},
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class AuthRoutingModule {}
但是我觉得所有这些“身份验证”都是多余的。有没有办法(甚至通过根应用程序路由模块)来指示此路由模块的所有规则之前都具有此auth
?
编辑 这是我的主模块路由(至少尝试一下):
import { Routes, RouterModule } from '@angular/router';
import { ModuleWithProviders } from '@angular/core';
export const routes: Routes = [
{ path: '', redirectTo:'/beers', pathMatch:'full' },
{ path: 'beers', loadChildren: './beers/beers.module#BeersModule'},
{ path: 'auth', loadChildren: './auth/auth.module#AuthModule'},
];
export const AppRoutes: ModuleWithProviders = RouterModule.forRoot(routes, {scrollPositionRestoration: 'enabled', enableTracing: true});
答案 0 :(得分:0)
这绝对有可能,如果没有完整的代码,很难建议您如何具体实现此目的,但是您将在app-routing.module
[
{
path: '',
component: App.AppComponent,
children: [
{ path: 'auth', loadChildren: './auth/auth.module#AuthModule' }
]
}
]
然后您的auth.module
中的路线如下:
const routes: Routes = [
{
path: "login",
component: LoginComponent
},
{
path: "signup",
component: SignupComponent
},
{
path: "logout",
component: LogoutComponent
},
];