angular - 为 Angular 4的不同页面设置不同布局的最佳方法

标签 angular angular-routing angular-template angular-router

我是 Angular 4 的新手。我想要实现的是为我的应用程序中的不同页面设置不同的布局页眉和页脚。我有三种不同的情况:

  1. 登录、注册页面(无页眉,无页脚)

路由:['登录','注册']

  1. 营销站点页面(这是根路径,它有页眉和页脚,大部分这些部分出现在登录之前)

路线:['','关于','联系']

  1. 应用程序登录页面(我在本节中为所有应用程序页面设置了不同的页眉和页脚,但此页眉和页脚不同于营销网站的页眉和页脚)

路线:['仪表板','个人资料']

我通过向路由器组件 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/

相关文章:

Angular 2 - 将变量传递给 ng-template

javascript - 在 Angular 分量中向谷歌地图添加标记

php - 来自 PHP 的 Angular 和日期 InvalidPipeArgument : 'Unable to convert "[object Object ]"into a date' for pipe 'DatePipe'

javascript - 意外的标记。构造函数、方法、访问器或属性的预期 Angular 为 5

angular - 使用 Cypress 测试 Angular 时如何防止整页重新加载?

angular - 仅在以 Angular 刷新页面后才会显示来自 api 的数据

javascript - AngularJs 指令 - &lt;script&gt; inside template

javascript - 如果工具提示模板为空,如何在Angular2中禁用NgbTooltip?

java - 在单页 Web 应用程序中的 Angular route 单击链接时如何触发或执行 http 服务?

angular - TemplateRef 类型检查/声明类型