我必须使用模块,在这些模块中,在通过auth
进行路由的过程中,the模块是延迟加载的。具有路由的主模块app
模块如下所示:
const app_routes: Routes = [
{ path: '', component: MainComponent, outlet: 'app', pathMatch: 'full',
canActivate: [OauthGuard]},
{ path: 'auth', loadChildren: 'app/authentication/authentication.module#AuthenticationModule'},
{ path: '**', component: PageNotFoundComponent, outlet: 'app' }
];
@NgModule({
declarations: [...],
imports: [RouterModule.forRoot(app_routes)],
providers: [OauthGuard],
bootstrap: [
AppComponent
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
exports: [RouterModule]
})
export class AppModule {}
在这里,导航到auth
加载AuthenticationModule
。
const auth_routes: Routes = [
{
path: '',
component: AuthenticationComponent,
outlet: 'app',
children: [
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{
path: 'register',
component: RegisterComponent,
outlet: 'auth'
},
{
path: 'login',
component: LoginComponent,
outlet: 'auth'
}
]}
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(auth_routes),
UtilsModule,
HttpModule
],
declarations: [
AuthenticationComponent,
RegisterComponent,
LoginComponent
],
exports: [
RouterModule
],
providers: [
ClientService,
AuthenticationService
],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
bootstrap: [
AuthenticationComponent
]
})
export class AuthenticationModule {
}
根据我的逻辑,当导航到LoginComponent
时,auth
应该被加载到auth/login
路由器-出口上.但相反,我得到了以下错误消息:
错误:无法匹配任何路由。URL段:‘auth/登录’
正如建议的那样,当canActivate()
在第一次路由更改时被调用时,我记录了已注册的路由,得到了以下结果:
Routes: [
{
"path": "",
"outlet": "app",
"pathMatch": "full",
"canActivate": [
null
]
},
{
"path": "auth",
"loadChildren": "app/authentication/authentication.module#AuthenticationModule"
},
{
"path": "**",
"outlet": "app"
}
]
注册的路由在àuthentication.module`构造函数的ran中是相同的。
我真的不知道为什么我还会犯这个错误。有小费吗?
发布于 2017-06-14 09:39:11
在一些帮助下,我解决了这个问题,那里的问题是出口名称。我不知道有可能有多个未命名的router-outlets
,只要它们属于不同的作用域。所以我所做的只是删除插座的名字。使用我的旧代码,我必须导航到/auth(auth:login)
,其中(auth:login)
指定要显示的出口名称和子路由。没有出口名称,我可以简单地导航到auth/login
,并到达所需的页面。
authentication.module
const routes: Routes = [
{
path: '',
component: AuthenticationComponent,
children: [
{ path: '', redirectTo: 'login', pathMatch: 'full'},
{
path: 'register',
component: RegisterComponent
},
{
path: 'login',
component: LoginComponent
}
]}
];
https://stackoverflow.com/questions/44507914
复制相似问题