【发布时间】:2017-09-30 18:12:46
【问题描述】:
我有一个主router-outlet,用于显示登录屏幕(/login)和主内容屏幕(登录后显示)(/main)。
一旦用户在内容屏幕上,我想在顶部显示一个导航栏,有 2 个选项(例如,“概览”、“见解”)。这个导航栏对于OverviewComponent 和InsightsComponent 很常见
在此导航栏下方,我想显示另一个路由器插座,它将根据用户在导航栏中的点击加载OverviewComponent 或InsightsComponent。如果我将“/overview”和/“insights”作为路线,它将直接向我显示相应的组件,而不是导航栏。
以下是我当前的路由配置(这是不对的):
const appRoutes: Routes = [
{ path: 'main', component: MainComponent},
{ path: 'overview', component: OverviewComponent},
{ path: 'insights', component: InsightsComponent },
{ path: 'login', component: LoginComponent },
{ path: '',
redirectTo: '/login',
pathMatch: 'full'
},
{ path: '**', component: PageNotFoundComponent }
];
如果我们可以在 angular2 angular4 中实现这一点,请告诉我。我正在使用以下版本:
"@angular/core": "^4.0.0"
"@angular/router": "^4.0.0"
"@angular/cli": "1.0.1"
******************尝试 2 - 仍然不工作******************
const appRoutes: Routes = [
{ path: 'main',
children: [
{ path: '', component: MainComponent },
{ path: 'overview', component: OverviewComponent },
{ path: 'insights', component: InsightsComponent },
]
},
{ path: 'login', component: LoginComponent },
{ path: '',
redirectTo: '/login',
pathMatch: 'full'
},
{ path: '**', component: PageNotFoundComponent }
];
*******尝试 2 - 包含所有组件的 Sudo 代码 - 仍然无法正常工作*******
//app.component.html
<router-outlet></router-outlet>
//app.module.ts
const appRoutes: Routes = [
{ path: 'main',
children: [
{ path: '', component: MainComponent },
{ path: 'overview', component: OverviewComponent },
{ path: 'insights', component: InsightsComponent },
]
},
{ path: 'login', component: LoginComponent },
{ path: '',
redirectTo: '/login',
pathMatch: 'full'
},
{ path: '**', component: PageNotFoundComponent }
];
//login.component.html
<div class="text-center vertical-center">
<form>
<div class="horizontal">
<label for="">Email</label>
<input class="form-control" type="text" name="" value="">
</div>
<div class="horizontal">
<label for="">Password</label>
<input class="form-control" type="password" name="" value="">
</div>
<button class="btn btn-primary" (click)="navigate()">Login</button>
</form>
</div>
//login.component.ts
navigate() {
this.router.navigate(['./main']);
}
//main.component.html
<app-header></app-header>
<router-outlet></router-outlet>
//app.header.html
<ul class="nav navbar-nav">
<li class=""><a routerLink="/main/overview" routerLinkActive="active">OVERVIEW</a></li>
<li class=""><a routerLink="/main/insights" routerLinkActive="active">INSIGHTS</a></li>
</ul>
//overview.html
<p>This is overview section</p>
//insights.html
<p>This is insights section</p>
********尝试 3 - 工作********
const appRoutes: Routes = [
{ path: 'main', component: MainComponent,
children: [
{ path: '', component: MainComponent },
{ path: 'overview', component: OverviewComponent },
{ path: 'insights', component: InsightsComponent },
]
},
{ path: 'login', component: LoginComponent },
{ path: '',
redirectTo: '/login',
pathMatch: 'full'
},
{ path: '**', component: PageNotFoundComponent }
];
【问题讨论】:
-
我可以看看你的 main.component.html 是什么样的吗?
-
<!--MainComponent.html--> <app-header></app-header> <router-outlet></router-outlet> <!--AppHeader.html--> <ul class="nav navbar-nav"> <li class=""><a href="/main/overview">OVERVIEW</a></li> <li class=""><a href="/main/insights">INSIGHTS</a></li> </ul> -
无法通过在app-header中添加routerLink解决?如果它不起作用,您会遇到什么错误? - 您可以使用特定代码编辑问题。会很清楚。-
-
您有两个选择:您可以定义子路由器插座或辅助(辅助)路由器插座。有关更多信息,请参阅文档:angular.io/docs/ts/latest/guide/router.html
-
嗨 Saiyaff,我确实在 href 之前尝试了 routerLink,但也没有用。没有抛出错误。当我单击“概览”或“洞察”时,屏幕会显示概览或洞察组件,但不包含主组件中存在的标题。
标签: angular angular-cli router router-outlet