简体   繁体   English

如何在Angular2中切换布局

[英]How to switch layouts in Angular2

What is the best practices way of using two entirely different layouts in the same Angular2 application? 在同一个Angular2应用程序中使用两个完全不同的布局的最佳实践方法是什么? For example in my /login route I want to have a very simple box horizontally and vertically centered, for every other route I want my full template with header, content and footer. 例如,在我的/登录路线中,我想要一个非常简单的水平和垂直居中的框,对于每一个其他路线,我想要我的完整模板与标题,内容和页脚。

In your main component html you can add the following routes outlet which you will use to switch layout. 在主要组件html中,您可以添加以下用于切换布局的路径插座。

<router-outlet name="header"></router-outlet>
<router-outlet name="navbar"></router-outlet>
<router-outlet></router-outlet>
<router-outlet name="footer"></router-outlet>

In this case you can configure your routes to switch the header, navbar if any and footer of your app when page changes. 在这种情况下,您可以配置路由以在页面更改时切换标题,导航栏(如果有)和页脚。 The following is an example of how you can configure your routes. 以下是如何配置路由的示例。

Example 1 Lets assume the first layout has only header and footer without any sidebar/navbar 示例1假设第一个布局只有页眉和页脚,没有任何侧栏/导航栏

export const welcome_routes: RouterConfig = [
  { path: 'firstpage', children:[
     { path: 'login', component: LoginComponent},
     { path: 'signup', component: SignupComponent},
     { path: '' , component: Header1Component, outlet: 'header'}
     { path: '' , component: Footer1Component, outlet: 'footer'}
  ]}
];

Example 2. This is your routes config for your second layout 示例2.这是您的第二个布局的路由配置

 export const next_layout_routes: RouterConfig = [
  { path: 'go-to-next-layout-page', children:[
     { path: 'home', component: HomeComponent},
     { path: '' , component: Header2Component, outlet: 'header'}
     { path: '' , component: NavBar2Component, outlet: 'navbar'}
     { path: '' , component: Footer2Component, outlet: 'footer'}
  ]}
];

With this its very easy to add a third and a fourth and a ... layout to your page. 有了这个,它很容易添加第三个和第四个和...布局到您的页面。

Hope this helps 希望这可以帮助

** Updated ** ** 更新 **

RouterConfig has been changed to Routes. RouterConfig已更改为Routes。

So the code above will now be 所以上面的代码现在将是

export const welcome_routes: Routes = [
  { path: 'firstpage', children:[
     { path: 'login', component: LoginComponent},
     { path: 'signup', component: SignupComponent},
     { path: '' , component: Header1Component, outlet: 'header'}
     { path: '' , component: Footer1Component, outlet: 'footer'}
  ]}
];

In Angular 4 (and probably also in Angular 2) you can do: 在Angular 4中(也可能在Angular 2中),您可以:

const routes: Route[] = [
  {path: 'admin', redirectTo: 'admin/dashboard', pathMatch: 'full'},
  {
    path: 'admin',
    children: [
      {
        path: '', component: DefaultLayoutComponent,
        children: [
          {path: 'dashboard', component: DashboardComponent}
        ]
      },
      {
        path: '',
        children: [
          {path: 'login', component: LoginComponent}
        ]
      }
    ]
  }
]

By using path: '' you won't have to invent different url namespaces in order to use a simple layout. 通过使用path: ''您不必为了使用简单的布局而发明不同的url名称空间。 This is what the views look like: 这就是视图的样子:

index.html: index.html的:

<router-outlet>

default-layout.html: 默认的layout.html:

<div class="sidebar"></div>
<div class="content">
  <router-outlet></router-outlet>
</div>

Another simple way is define children routes: 另一种简单的方法是定义子路径:

const appRoutes: Routes = [
  {
    path: 'fullLayout',
    component: FullLayoutComponent,
    children: [
      { path: 'view', component: HomeComponent },
    ]
  }, {
    path: 'simpleLayout',
    component: SimpleLayoutComponent,
    children: [
      { path: 'view', component: HomeComponent },
    ]
  }
];

/fullLayout/view - shows full layout structure / fullLayout / view - 显示完整的布局结构

/simpleLayout/view - shows simple layout structure / simpleLayout / view - 显示简单的布局结构

app.component.html app.component.html

<router-outlet></router-outlet>

full-layout.component.html 全layout.component.html

<h1>Full layout</h1>
<router-outlet></router-outlet>
<h1>FOOTER</h1>

simple-layout.component.html 简单layout.component.html

<h1>Simple layout</h1>
<router-outlet></router-outlet>

I strongly recommend reading the tutorial by the Angular team on routing here . 我强烈建议您在此处阅读Angular团队的教程。

There is some setup that you will need to implement, but some of the key steps are: 您需要实现一些设置,但一些关键步骤是:

  • Create an app.routes.js file where you specify the route name and the component that should be loaded when that route is hit. 创建一个app.routes.js文件,您可以在其中指定路径名称以及命中该路径时应加载的组件。

     import { provideRouter, RouterConfig } from '@angular/router'; import { LoginComponent } from 'components/login.component' const routes: RouterConfig = [ { path: '/login', //name of the route component: LoginComponent //component to load when route is hit } ]; export const appRouterProviders = [ provideRouter(routes) ]; 
  • In the master component where you will click a link (ie the nav-bar), you will need to add [routerLink]= 'myRouteName' (eg '/login') and a pair of router-outlet tags which is where the new component (aka view) will be inserted. 在您将单击链接(即导航栏)的主组件中,您需要添加[routerLink] ='myRouteName'(例如'/ login')和一对路由器插座标签,这是新的将插入组件(也称为视图)。

     import { Component } from '@angular/core'; import { ROUTER_DIRECTIVES } from '@angular/router'; @Component({ selector: 'nav-bar', template: ` <h1>{{title}}</h1> <a [routerLink]="['/login']">Login</a> <router-outlet></router-outlet> `, directives: [ROUTER_DIRECTIVES] }) export class AppComponent { } 

Hope that helps. 希望有所帮助。 I've been learning angular 2 over the last month, and the documentation on angular.io has been invaluable. 我上个月一直在学习角度2,而angular.io的文档非常宝贵。 Really clear, step-by-step explanations on a lot of aspects, and a good first stop when getting into a new topic. 对很多方面进行真正清晰,循序渐进的解释,并在进入新主题时获得良好的第一站。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM