[英]The child row is not accessible in child component using Angular 7
I just started to learn Angular 7 and I'm a little confused about routing. 我刚开始学习Angular 7,对路由感到有些困惑。 I created a route configuration in app-routing module and child row in message-routing module.
我在应用程序路由模块中创建了路由配置,在消息路由模块中创建了子行。
I expect these routes: 我希望这些路线:
/
/message
/message/inbox
/message/new
/about
But routes inside messageComponent are not accessible: 但是无法访问messageComponent内部的路由:
Cannot match any routes.
无法匹配任何路线。 URL Segment: 'message/new'
网址段:“消息/新”
These are my configurations: 这些是我的配置:
app-routing.module.ts: app-routing.module.ts:
const routes: Routes = [
{
path: '',
component: HomeComponent
},
{
path: 'message',
component: MessageComponent
},
{
path: 'about',
component: AboutComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
message-routing.module.ts: message-routing.module.ts:
const routes: Routes = [
{
path: 'message',
component: MessageComponent,
children: [
{
path: '',
component: InboxComponent
},
{
path: 'new',
component: NewComponent
}
]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
app.module.ts: app.module.ts:
@NgModule({
declarations: [
AppComponent,
MessageComponent,
HomeComponent,
AboutComponent,
],
imports: [
BrowserModule,
MessageRoutingModule,
AppRoutingModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
And this is my folder structure: 这是我的文件夹结构:
Here is an example : 这是一个例子:
child-routing.module.ts child-routing.module.ts
const routes: Routes = [
{
path: 'child,
component: ChildComponent
canActivate: [YourGuard],
children: [{
path: '',
component: ChildAComponent
},
{
path: 'some-route',
component: ChildBComponent
}]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
providers: [YourGuard],
exports: [RouterModule]
})
export class ChildRoutingModule { }
app-routing.module.ts app-routing.module.ts
const routes: Routes = [
{
path: 'other-route,
component: OtherComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
And in app.module.ts : 并在app.module.ts中 :
...
import:[
ChildRoutingModule,
AppRoutingModule
]
I solved the problem by Adding Inbox and New components to the message-routing.module.ts. 我通过将Inbox和New组件添加到message-routing.module.ts解决了该问题。 I'm not sure if here is the best place to add them but this solved the problem.
我不确定这是否是添加它们的最佳位置,但这解决了问题。
message-routing.module.ts: message-routing.module.ts:
const routes: Routes = [
{
path: 'message',
component: MessageComponent,
children: [
{
path: '',
component: InboxComponent
},
{
path: 'new',
component: NewComponent
}
]
}
];
@NgModule({
declarations: [InboxComponent, NewComponent],
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.