【问题标题】:Angular2 - Shared Layout for multiple modulesAngular2 - 多个模块的共享布局
【发布时间】:2018-02-26 13:06:35
【问题描述】:

我用不同的模块构建了一个 Angular 应用程序。每个模块处理不同的任务。在我的登录页面上,用户应该登录或注册。这是一个非常精简的布局,没有任何导航。在我的功能模块(登录后可访问)上,用户可以执行任务和搜索信息。

我的主要问题是,我的功能模块应该共享相同的布局(导航、工具栏等),而我的 AuthModule 不应该有相同的布局。下图应该说明我试图实现的目标。

每个模块(Auth 和 Features)都有自己的 RoutingModule 和不同的组件集。对 Layout2 和 FeatureModules 的访问受到 AuthService/AuthGuards 的保护。

我已经为基于组件的设置(https://stackoverflow.com/a/40508804/1365061https://stackblitz.com/edit/angular-multi-layout-example?file=app%2Fapp.routing.ts)找到了很好的解决方案,但对于基于模块的设置却没有。我想延迟加载功能模块,这不能应用于我找到的给定解决方案(至少它不适用于模块)。

如何在模块之间共享布局,或者换句话说“在布局组件中加载模块”?

我目前的代码sn-ps是:

app.component

<router-outlet></router-outlet>

user-area.component

<mat-toolbar color="primary" class="mat-elevation-z2" *ngIf="(auth.account$ | async) as account">
    <button mat-button class="pull-left" [disabled]="!account" (click)="sidenav.toggle()">
        <i class="fa fa-navicon" aria-hidden="true"></i> SiteName
    </button>

    <button mat-button class="pull-left ml-3 pull-left" routerLink="/settings/profile">
        <img [src]="account.userID | avatar" class="rounded-circle mr-1" width="30" height="30">
        <span class="d-none d-sm-inline"> {{account.name}}</span>
    </button>

    <button id="logoutButton" mat-button class="pull-right" routerLink="/auth/logout">
        <i class="fa fa-power-off" aria-hidden="true"></i><span class="d-none d-sm-inline"> Logout</span>
    </button>
</mat-toolbar>

<mat-sidenav-container (window:resize)="onResize($event)" [style]="mainStyle.getValue()">
    <mat-sidenav *ngIf="auth.account$ | async" [mode]="sidenavMode" [opened]="true" #sidenav class="sidenav-shadow">
        <app-nav-pane *ngFor="let nav of (app.navmods$ | async)" [content]="nav"></app-nav-pane>
    </mat-sidenav>
    <div class="container-fluid">
        <div class="row" style="flex: 1 0 auto;">
            <div class="col-12">

                <router-outlet></router-outlet>
            </div>
        </div>
    </div>
    <app-footer *ngIf="responsiveService.mdOrLarger"></app-footer>
</mat-sidenav-container>

public.component

<div class="..." style="...">
   <router-outlet></router-outlet>
</div>

我的 app.routing.module 路由

const routes: Routes = [
    {
        path: 'auth',
        component: PublicAuthComponent,
        canActivate: [NotLoggedInGuard]
    },
    {
        path: '',
        component: UserAreaComponent,
        canActivate: [LoggedInGuard]
    }
];

如前所述,我尝试使用上述链接中的概念,但没有按预期工作。

【问题讨论】:

标签: angular layout module routing


【解决方案1】:

您可以使用父/子路由来实现此目的。这样的东西应该可以工作(可能需要一些调整取决于您项目的其他部分)

const routes: Routes = [
    {
        path: 'auth',
        component: Layout1
        canActivate: [NotLoggedInGuard],
        children: [
           { path: '', component: PublicAuthComponent }
        ]
    },
    {
        path: '',
        component: Layout2,
        canActivate: [LoggedInGuard],
        children: [
          { path: '', loadChildren: './path/to/module/feature.module#FeatureModule' }
        ]
    }
];

记得把&lt;router-outlet&gt;放在两个布局组件里面。

【讨论】:

  • 如前所述,我在每个模块中使用模块路由(每个模块有不同的路由模块)。所以,据我了解,我不能使用这个路由概念。
  • 我已经更新了我的建议,所以现在路径指向使用自己的路由的功能模块。
  • 应该是模块本身的路径还是路由模块的路径?我有多个 Fe​​atureModule,创建一个处理所有“非身份验证路由”的“FeatureModuleRoutingModule”有用吗?
  • 这是(延迟加载)模块本身的路径。我会为每个功能模块使用单独的路由模块。根据我的经验,它更容易维护和重构。但我不知道您的应用程序整体看起来如何,因此您需要自己做出决定。
  • 好的,这行得通。我的错误是我没有使用“loadChildren”路由参数。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
  • 2017-11-23
相关资源
最近更新 更多