【发布时间】:2020-01-29 14:14:15
【问题描述】:
我想在 Angular 8 应用程序中为页眉、页脚、左侧和主要内容制作通用布局。
我不想每次当用户从一个页面导航到另一个页面时重新加载页眉、页脚和左侧,除了一些页面,如登录,请联系我们。
我的一些基本代码如下所示
app.componant.html
<div>
<router-outlet name="header"></router-outlet>
<div>
<router-outlet name="leftMenu"></router-outlet>
<div>
<router-outlet name="content"></router-outlet>
</div>
</div>
<router-outlet name="footer"></router-outlet>
</div>
app.routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HeaderComponent } from './header/header.component';
import { FooterComponent } from './header/footer.component';
import { LeftMenuComponent } from './header/left-menu.component';
const routes: Routes = [
{ path: '',
component: HeaderComponent,
outlet: "header",
},
{ path: '',
component: HeaderComponent,
outlet: "footer",
},
{ path: '',
component: HeaderComponent,
outlet: "leftMenu",
},
{ path: 'login',
loadChildren: () => import('./login/login.module').then(mod => mod.LoginModule)
},
{ path: 'home',
loadChildren: () => import('./home/home.module').then(mod => mod.HomeModule)
},
{ path: '',
redirectTo: '/home',
pathMatch: 'full',
},
//{ path: '**', component: PageNotFoundComponent }
];
@NgModule({
imports: [
//EmptyModule,
RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
login.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LoginComponent } from './login.component';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{
path: '',
component: LoginComponent,
outlet: "content",
children: [
{
path: '', component: null,
outlet: "header",
},
{
path: '', component: null,
outlet: "footer",
},
{
path: '', component: null,
outlet: "leftMenu",
},
// { path: '**', component: Page404balanceComponent}
]
}
];
@NgModule({
imports: [
CommonModule,
//EmptyModule,
RouterModule.forChild(routes)
],
declarations: [LoginComponent]
})
export class LoginModule { }
在主页路由中,它适用于页眉、左侧菜单、内容和页脚。 但在登录时我不想显示页眉、左侧菜单和页脚。
在 ui-router 中,我们可以使用 @like 作为 @content, @header 来覆盖父继承路由,但我不能像那样覆盖。
所以,请帮助我制作这种类型的布局,例如 ui-router。
【问题讨论】:
-
为什么不在 app.component.html 中渲染
<app-header>而不是<router-outlet>? -
您在尝试使用这么多路由器插座时陷入了痛苦的世界。 Ui 路由器正在解决 angularjs 中的一个问题,而 angular 2+ 中的组件在很大程度上解决了这个问题。您的应用设计需要调整
-
@bryan60 :我已经通过使用多个 ui 视图在 angular 7 中使用了 ui 路由器,它工作正常,但是在我的 angular 8 新项目中,我只想通过 angualr 2+ 默认路由进行路由。我可以通过对页眉、页脚和左侧菜单使用条件组件来处理,但问题是我需要将所有路由条件都放在它上面。所以我想如果它通过使用继承路由概念来完成,那就很好了。
标签: angular angular-ui-router angular2-routing multipleoutputs