Angular2 - 多个模块共享布局

3

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

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

enter image description here

每个模块(Auth和Features)都有自己的RoutingModule和不同的组件集。访问Layout2和FeatureModules受AuthService / AuthGuards保护。
我已经为基于组件的设置找到了好的解决方案(https://dev59.com/fFkS5IYBdhLWcg3wASLc#40508804https://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]
    }
];

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


您可以参考以下链接:https://dev59.com/sVgR5IYBdhLWcg3wtvRr - Trunghieu Le
1个回答

5
您可以使用父/子路由来实现此目的。类似以下代码应该可以工作(根据您项目的其他部分可能需要进行一些调整):
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>标签。

就像之前所说,我在每个模块中都使用模块路由(为每个模块创建单独的路由模块)。因此,据我所知,我无法使用这种路由概念。 - Alex
我已经更新了我的提案,现在路径指向的是功能模块,该模块使用自己的路由。 - Mateusz Witkowski
这应该是模块本身还是路由模块的路径?我有多个FeatureModules,创建一个“FeatureModuleRoutingModule”来处理所有“not auth routings”是否有用? - Alex
这是指向(惰性加载的)模块本身的路径。我会为每个功能模块使用单独的路由模块。根据我的经验,这样更容易维护和重构。但我不知道你的整个应用程序是什么样子的,所以你需要自己做出决定。 - Mateusz Witkowski
好的,这个已经可以了。我的错误在于我没有使用“loadChildren”路由参数。谢谢。 - Alex
这意味着功能模块将被惰性加载。如果您不想使用惰性加载,恐怕没有其他办法。 - jbojcic

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接