如何在angular2中使用多个路由器插座? [英] How to use multiple router-outlet in angular2?

查看:121
本文介绍了如何在angular2中使用多个路由器插座?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主router-outlet,用于显示登录屏幕(/login)和主内容屏幕(在登录后显示)(/main).

I have a main router-outlet, which is used to display a login screen (/login) and the main content screen(which is shown after login) (/main).

一旦用户位于内容屏幕上,我想在顶部显示一个导航栏,其中有2个选项(例如,概述",洞察").此导航栏对于OverviewComponentInsightsComponent

Once the user is on content screen, I want to show a nav-bar on top, with 2 options(say, 'Overview', 'Insights'). This nav-bar is common for OverviewComponent and InsightsComponent

在此导航栏下面,我想显示另一个路由器出口,根据用户在导航栏中单击的内容,该路由器出口将加载OverviewComponentInsightsComponent.如果我将"/overview"和/"insights"作为路线,它将直接显示相应的组件,而不是导航栏.

Below this nav-bar, I want to show another router-outlet, which would load OverviewComponent or InsightsComponent, based on what user clicks in nav-bar. If I give '/overview' and /'insights' as the route, it would directly show me the respective component, but not the nav-bar.

以下是我当前的路由配置(不正确):

Following is my current routing config (this is not right):

const appRoutes: Routes = [
  { path: 'main', component:  MainComponent},
  { path: 'overview', component:  OverviewComponent},
  { path: 'insights', component: InsightsComponent },
  { path: 'login', component: LoginComponent },
  { path: '',
    redirectTo: '/login',
    pathMatch: 'full'
  },
  { path: '**', component: PageNotFoundComponent }
];

请让我知道我们是否可以在angular2 angular4中实现这一目标.我正在使用以下版本:

Kindly let me know if we can achieve this in angular2 angular4. I'm using following version:

"@angular/core": "^4.0.0"
"@angular/router": "^4.0.0"
"@angular/cli": "1.0.1"

******************尝试2-仍无法正常工作******************

******************Attempt 2 - Still not working******************

const appRoutes: Routes = [
  { path: 'main',
    children: [
      { path: '', component: MainComponent },
      { path: 'overview', component:  OverviewComponent },
      { path: 'insights', component: InsightsComponent },
    ]

  },
  { path: 'login', component: LoginComponent },
  { path: '',
    redirectTo: '/login',
    pathMatch: 'full'
  },
  { path: '**', component: PageNotFoundComponent }
];

*******尝试2-具有所有组件的Sudo代码-仍无法正常工作*******

*******Attempt 2 - Sudo Code with all components - Still not working*******

//app.component.html

<router-outlet></router-outlet>


//app.module.ts

const appRoutes: Routes = [
  { path: 'main',
    children: [
      { path: '', component: MainComponent },
      { path: 'overview', component:  OverviewComponent },
      { path: 'insights', component: InsightsComponent },
    ]

  },
  { path: 'login', component: LoginComponent },
  { path: '',
    redirectTo: '/login',
    pathMatch: 'full'
  },
  { path: '**', component: PageNotFoundComponent }
];

//login.component.html
<div class="text-center vertical-center">
    <form>
        <div class="horizontal">
            <label for="">Email</label>
            <input class="form-control" type="text" name="" value="">
        </div>
        <div class="horizontal">
            <label for="">Password</label>
            <input class="form-control" type="password" name="" value="">
        </div>
        <button class="btn btn-primary" (click)="navigate()">Login</button>
    </form>
</div>

//login.component.ts
navigate() {
    this.router.navigate(['./main']);
  }

//main.component.html

<app-header></app-header>
<router-outlet></router-outlet>


//app.header.html

<ul class="nav navbar-nav">
               <li class=""><a routerLink="/main/overview" routerLinkActive="active">OVERVIEW</a></li>
                <li class=""><a routerLink="/main/insights" routerLinkActive="active">INSIGHTS</a></li>
            </ul>

//overview.html
<p>This is overview section</p>

//insights.html
<p>This is insights section</p>

********尝试3-工作中**********

********Attempt 3 - Working**********

const appRoutes: Routes = [
  { path: 'main', component: MainComponent,
    children: [
      { path: '', component: MainComponent },
      { path: 'overview', component:  OverviewComponent },
      { path: 'insights', component: InsightsComponent },
    ]

  },
  { path: 'login', component: LoginComponent },
  { path: '',
    redirectTo: '/login',
    pathMatch: 'full'
  },
  { path: '**', component: PageNotFoundComponent }
];

推荐答案

因此,如果我正确地回答了这个问题,那么您希望最初具有登录屏幕,并且在用户登录后,您希望他查看显示导航的/main.登录屏幕和主应用程序应具有不同的布局.

So if I get the question right you want to have login screen initially and after the user logs in, you want him to see /main where navigation is shown. Both login screen and main application should have a different layout.

我们有一个类似的案例,并使用LayoutComponent.这是简化的示例.

We have a similar case and using LayoutComponent. Here's simplified example.

// This is main component that get's bootstrapped that has 'top-level' router.
@Component({selector: 'app', template: '<router-outlet></router-outlet>'})
class AppComponent {}

// main router config
// Here AuthModule has router with login and logout configured and LoginGuard
// redirect the user to /auth/login when she is not authenticated.
// We're using lazy-loading but you can use direct component instead
export const APP_ROUTES: Routes = [
  {path: '', redirectTo: 'main', pathMatch: 'full'},
  {path: 'auth', loadChildren: '../modules/+auth/auth.module#AuthModule'},
  {
    path: '',
    component: LayoutComponent,
    canActivate: [LoginGuard],
    children: [
      {path: 'main', loadChildren: '../modules/+main/main.module#MainModule'}
    ]
  }
];

// AuthModule/LoginComponent has own template and it will be rendered
// into 'top-level' router-outlet.

// LayoutComponent
// Here you define your main application layout that can include navigation
// and anything else that are global to the app. It has another router-outlet
// that get rendered when the layout is accessible (which in this case when the user is authenticated).
@Component({
  selector: 'app-layout',
  template: `
    <div id="wrapper">
      <app-sidebar></app-sidebar>
      <div id="page-wrapper" class="gray-bg dashboard-1" adjust-content-height>
        <router-outlet></router-outlet>
      </div>
    </div>
    <notifications></notifications>
    <error-modal></error-modal>
  `
})
export class LayoutComponent {}

// Auth/LoginComponent can have its own template that will have different layout from the main application

因此流程如下:

  • 当用户尝试加载/时,她将重定向到/main
  • 如果用户未通过身份验证,则将其重定向到/auth/login,否则将加载/main

希望有帮助.

使用示例应用程序更新了 sickelap/ng-starter 存储库,该示例应用程序具有:

Updated sickelap/ng-starter repository with example app that has:

  • 通过延迟加载进行路由
  • 布局
  • 和其他东西

这篇关于如何在angular2中使用多个路由器插座?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆