我是 Angular 4 的新手。我想要实现的是为我的应用程序中的不同页面设置不同的布局页眉和页脚。我有三种不同的情况:
- 登录、注册页面(无页眉,无页脚)
路由:['登录','注册']
- 营销站点页面(这是根路径,它有页眉和页脚,大部分这些部分出现在登录之前)
路线:['','关于','联系']
- 应用程序登录页面(我在本节中为所有应用程序页面设置了不同的页眉和页脚,但此页眉和页脚不同于营销网站的页眉和页脚)
路线:['仪表板','个人资料']
我通过向路由器组件 html 添加页眉和页脚来临时运行该应用程序。
请告诉我一个更好的方法。
这是我的代码:
app\app.routing.ts
const appRoutes: Routes = [
{ path: '', component: HomeComponent},
{ path: 'about', component: AboutComponent},
{ path: 'contact', component: ContactComponent},
{ path: 'login', component: LoginComponent },
{ path: 'register', component: RegisterComponent },
{ path: 'dashboard', component: DashboardComponent },
{ path: 'profile', component: ProfileComponent },
// otherwise redirect to home
{ path: '**', redirectTo: '' }
];
export const routing = RouterModule.forRoot(appRoutes);
app.component.html
<router-outlet></router-outlet>
app/home/home.component.html
<site-header></site-header>
<div class="container">
<p>Here goes my home html</p>
</div>
<site-footer></site-footer>
app/about/about.component.html
<site-header></site-header>
<div class="container">
<p>Here goes my about html</p>
</div>
<site-footer></site-footer>
app/login/login.component.html
<div class="login-container">
<p>Here goes my login html</p>
</div>
app/dashboard/dashboard.component.html
<app-header></app-header>
<div class="container">
<p>Here goes my dashboard html</p>
</div>
<app-footer></app-footer>
我看到了this question关于堆栈溢出,但我没有从那个答案中得到清晰的图片
最佳答案
您可以使用子路由解决您的问题。
在 https://angular-multi-layout-example.stackblitz.io/ 查看工作演示或编辑https://stackblitz.com/edit/angular-multi-layout-example
像下面这样设置你的路线
const appRoutes: Routes = [
// Site routes goes here
{
path: '',
component: SiteLayoutComponent,
children: [
{ path: '', component: HomeComponent, pathMatch: 'full'},
{ path: 'about', component: AboutComponent }
]
},
// App routes goes here
{
path: '',
component: AppLayoutComponent,
children: [
{ path: 'dashboard', component: DashboardComponent },
{ path: 'profile', component: ProfileComponent }
]
},
// no layout routes
{ path: 'login', component: LoginComponent},
{ path: 'register', component: RegisterComponent },
// otherwise redirect to home
{ path: '**', redirectTo: '' }
];
export const routing = RouterModule.forRoot(appRoutes);
关于angular - 为 Angular 4的不同页面设置不同布局的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46527826/