代码之家  ›  专栏  ›  技术社区  ›  Alex Multifabrika

Angular2-多个模块的共享布局

  •  3
  • Alex Multifabrika  · 技术社区  · 7 年前

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

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

    enter image description here

    每个模块(身份验证和功能)都有自己的路由模块和不同的组件集。对布局2和功能模块的访问受AuthService/AuthGuards保护。

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

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

    我当前的代码段包括:

    应用程序。组成部分

    <router-outlet></router-outlet>
    

    用户区域。组成部分

    <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>
    

    平民的组成部分

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

    我的 应用程序。路由。模块布线 :

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

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

    1 回复  |  直到 7 年前
        1
  •  5
  •   Mateusz Witkowski    7 年前

    可以使用父/子布线来实现这一点。像这样的东西应该可以工作(可能需要一些调整,具体取决于项目的其他部分)

    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' }
            ]
        }
    ];
    

    记住把 <router-outlet> 两个布局组件内部。