我刚接触Angular 4.我想要实现的是在我的应用程序中为不同的页面设置不同的布局页眉和页脚.我有三个不同的案子:

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

路由:[‘LOGIN’,‘REGISTER’]

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

路由:['','about','contact']

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

路由:[‘仪表板’,‘配置文件’]

我通过向我的路由组件html添加页眉和页脚来临时运行该应用程序.

请给我建议一个更好的方法.

这是我的代码:

app\app.路由.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);

应用程序.组成部分html

<router-outlet></router-outlet>

应用程序/主页/主页.组成部分html

<site-header></site-header>
<div class="container">
    <p>Here goes my home html</p>
</div>
<site-footer></site-footer>

app/about/about.组成部分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 混合应用程序-模型未更新 Select

从Observatory的订阅方法外正确接收JSON数据

如何在投影项目组周围添加包装元素?

Angular -移位在2个示波器中读取

使用jsPDF生成带有图像的PDF时出现灰色条纹

我应该对我想要在我的Angular 17+组件中显示的任何变量使用信号吗?

Angular react 形式验证问题

Angular 单调服务已多次创建

具有两个订阅方法的 Angular 16 takeUntilDestroyed 运算符

Angular 15 Ref 错误:初始化前无法访问组件 A

Angular 在关闭另一个对话框后打开另一个对话框的最佳做法是什么?

Angular6:RxJS switchMap 运算符未按预期工作

模块内的命名路由出口 - 路径匹配但内容未加载

如果我使用 matAutocomplete,为什么 textarea 不显示 formControl 的实际值

将 injectiontoken 用于 Angular 中的模块特定设置

Rxjs Observable:仅为第一个订阅者获取 HTTP 数据,为其余订阅者获取缓存数据

rxjs 这种常见的状态管理模式有名称吗?它有限制吗?

Angular 2 二传手与 ngOnChanges

在 Ionic 2 中使用图像资源的正确方法

Angular 2 - 什么是Root Scope?