Angular2 - 多个模块的共享布局

标签 angular layout module routing

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

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

enter image description here

每个模块(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)找到了很好的解决方案,但不是基于模块的设置。我想延迟加载功能模块,这不能应用于我找到的给定解决方案(至少它不适用于模块)。

我该怎么做才能在模块之间共享布局,或者换句话说“在布局组件中加载模块”?

我当前的代码片段是:

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

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

最佳答案

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

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>在两个布局组件中。

关于Angular2 - 多个模块的共享布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48989260/

相关文章:

c - 将 V=s Flage 传播到所有子 makefile

javascript - 最小 Angular2 应用程序接收未处理的 Promise SystemJS 错误

javascript - 如何从 index.html 中的动态脚本中删除内容

c# - 如何取消以编程方式添加的菜单的垂直空间?

java - GridView 不符合 Relativelayout 对齐方式

perl - 从模块导出 perl 函数的错误方法

python - 在模块命名空间中填充值

angular - Ngx-datatable cellClass 不工作

html - 如何在单击按钮时通过 Angular 7 进行自动对焦

android - 为什么我不能将右对齐的 TextView 放入 TableView 中?