首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Angular 2中使用router-outlet

在Angular 2中使用router-outlet
EN

Stack Overflow用户
提问于 2016-11-21 13:19:36
回答 3查看 35.6K关注 0票数 5

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

代码语言:javascript
复制
<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>

当用户单击" About“链接时,About组件显示在"main”路由出口中,但我的目的是将其显示在"home“路由器出口中,需要进行修改才能实现这一点。

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

根模块路由如下..

代码语言:javascript
复制
RouterModule.forRoot([
    {path:'' , component:LoginComponent },
    {path:'home' , component:HomeComponent}
  ]),

功能模块路由如下所示。

代码语言:javascript
复制
RouterModule.forChild([
    {path:'about' , component:AboutComponent }
])
EN

回答 3

Stack Overflow用户

发布于 2016-11-21 13:25:46

对于您的路由,请遵循此模式。

代码语言:javascript
复制
 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
        }
        ]
    }];
票数 3
EN

Stack Overflow用户

发布于 2017-11-30 07:22:32

1:不要在< -outlet>& < /router-outlet>之间嵌套路由器内容 "router-outlet“在功能上是一种i-frame。将内容放在那里是没有意义的,而且还没有看到这样做的教程。

2:如果有:< router-outlet name="MAIN_OUTLET_ID“>

然后,您需要在app.module.ts中使用类似以下内容

代码语言:javascript
复制
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的歧义。

票数 1
EN

Stack Overflow用户

发布于 2016-11-21 13:34:47

代码语言:javascript
复制
<router-outlet name="home"> 
  <a routerLink="about">About</a>
</<router-outlet>

你可以试试这个way.your 'about‘应该包含在主页中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40713246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档