繁体   English   中英

在Angular 2中使用路由器出口

[英]Using router-outlet in Angular 2

下面的伪代码描述了我的组件树结构。

<app-root>  

<router-outlet name="main"> 

     <Home-component>       

        <a routerLink="about">About</a> //
        <router-outlet name="home"> </<router-outlet>

     </Home-component>

</router-outlet>

</app-root>

当用户单击“关于”链接时,“关于组件”将显示在“主”路由出口中,但我的意图是将其显示在“主”路由器出口中,需要进行修改才能实现此目的。

“ app-root”组件和“ Home-component”是根模块的一部分,“ AboutComponent”是功能模块的一部分。

根模块路由如下。

RouterModule.forRoot([
    {path:'' , component:LoginComponent },
    {path:'home' , component:HomeComponent}
  ]),

功能模块路由如下...

RouterModule.forChild([
    {path:'about' , component:AboutComponent }
])

遵循此路线模式。

 export const routes: Route[] = [{
        path: '',
        redirectTo: "login",
        pathMatch: "full"
    }, 
    {
        path: 'login',
        component: LoginComponent
    }, 
    {
        path: 'csvtemplate',
        component: TemplateComponent,
        canActivate: ['canActivateForLoggedIn'],
        children: [{
            path: '',
            redirectTo: 'dashboard'
        }, 
        {
            path:'dashboard',
            component: DashboardComponent
        },
        {
            path: 'csvtimeline',
            component: CsvTimelineComponent
        }, {
            path: 'addcategory',
            component: CsvAddCategoryComponent
        }
        ]
    }];

1: 不要<router-outlet></ router-outlet>之间嵌套内容。“ router-outlet”是一种i型框架。 将内容放在那里毫无意义,并且已经看过零篇教程。

2:如果您具有: <路由器出口名称=“ MAIN_OUTLET_ID”>

然后,您需要在app.module.ts添加类似内容

const appRoutes: Routes=[
 {path: 'main', outlet:MAIN_OUTLET_ID, component:MainComponent},
 /// ... more routes here ... ///
]

@NgModule({
...
imports: [
    RouterModule.forChild([ appRoutes ]),
    /// ... other imports here ... ///
],
...
})

笔记:

1:在我的示例中,我使用“ MAIN_OUTLET_ID”,因为它使路径与路由器出口的ID保持一致。

<router-outlet name="home"> 
  <a routerLink="about">About</a>
</<router-outlet>

您可以尝试这种方式。您的“关于”应该包含在家里。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM