我正在构建一个有角度的5应用程序,而且我在玩路线方面遇到了麻烦......
我有2个主布局,每个布局都有如下所示的子路径:
SiteLayoutComponent - HomeComponent AppLayoutComponent - ContactComponent
我的问题在于默认路由,当应用程序第一次加载时,使用空URL,路由器必须重定向到Home Component,它具有子路由&#34; SiteLayoutComponent&#34;,这里的问题是它只加载HomeComponent而不是#34; SiteLayoutComponent&#34;它有一个<navbar-component>
。它仅在浏览器中输入/ home时有效。以下是我的路线:
const appRoutes: Routes = [
{
path: '',
redirectTo: 'home',
pathMatch: 'full'
},
//Site routes goes here
{
path: 'home',
component: SiteLayoutComponent,
children: [
{
path: '',
component: HomeComponent
}
]
},
//App routes goes here
{
path: 'app',
component: AppLayoutComponent,
children: [
{ path: 'contact', component: ContactComponent }
]
},
//No layout routes
{
path: '**', redirectTo: 'home'
}
];
export const routing = RouterModule.forRoot(appRoutes,{enableTracing:true});
它应该显示带有SiteLayoutComponent的HomeComponent(其中包含导航栏),但在键入类似&#34; http://localhost:4200/home&#34;的网址时,它只显示SiteLayoutComponent(包含导航栏)。 / p>
SiteLayoutComponent
<layout-site-navbar></layout-site-navbar>
<router-outlet></router-outlet>
SiteNavbarComponent
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li><a href="#">Link</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
请帮帮我!提前谢谢......
答案 0 :(得分:2)
可能是因为SiteLayoutComponent
和HomeComponent
具有相同的路径i-e / home。
使用具有以下内容的一个组件
<layout-site-navbar></layout-site-navbar>
<h1>THIS IS THE HOME PAGE<h1>
或者如下所示定义您的路线
{ path: 'home', component: SiteLayoutComponent,
children: [
{ path: '', redirectTo: 'detail', pathMatch: 'full' },
{ path: 'detail', component: HomeComponent}
]
}
答案 1 :(得分:0)
让我们说您有一个要路由的父级,要显示一些信息,在此信息下,根据路由器配置,一次要显示两个组件。这就是我们可以做到的。空路径路由是关键。
`
{
path: 'parent',
component: ParentComponent,
children: [
{
path: '',
redirectTo: 'defaultChild',
pathMatch: 'full'
},
{
path: 'defaultChild',
component: ChildComponent
},
{
path: 'otherChild',
component: OtherChildComponent
}]
}