简体   繁体   English

如何延迟加载和canActive模块与子路由

[英]how to lazy load and canActive a module with children routes

My idea begun when I thought of using a login view to protect my entire app ( using guards ) and force the user to login if he wants to navigate the main.component (planet.data in this case ). 当我想到使用登录视图保护我的整个应用程序(使用guards)并强迫用户登录到main.component(在这种情况下为planet.data)时,我的想法就开始了。

So I used lazy load ( not relevant ) to planet.module and canActivate inside planet-routing.module.ts but after that I couldn't navigate to planet-detail because the route cannot match the URL segment. 因此,我在Planet-routing.module.ts中使用了懒惰负载(无关)到planet.module和canActivate,但是在那之后我无法导航到planet-detail,因为路由无法匹配URL段。

"I solved" this issue adding planet-detail route as a children of 'rovers'. “我解决了”这个问题,将行星详细路线添加为“漫游者”的孩子。 Now it comes the part when I put router-outlet inside planet-data and everything it's ok until I click a planet image to navigate to that image ID. 现在,当我将路由器出口放入planet-data中时,一切就可以了,直到我单击一个行星图像导航到该图像ID为止,一切正常。 The image ID is right below the planet data view. 图片ID位于行星数据视图的正下方。 What im missing ?. 我缺少什么? I'd like to you point me in the right direction please. 我想请您指出正确的方向。

Expected behavior : Navigate to Image ID ( only that route ) 预期的行为:导航到图像ID(仅该路由) 在此处输入图片说明

// planet-data.component.html // planet-data.component.html

<div class="ui-g">
    <div class="ui-g-12 ui-md-12">
        <div class="ui-g-8">
            <app-title *ngIf="pics"></app-title>
        </div>
        <div class="ui-g-4">
            <app-dropdown-menu (selected)="onSelect($event)"></app-dropdown-menu>
        </div>
    </div>
    <div class="ui-g-12">
        <app-no-image class="margin" [cam]="true" [start]="true" *ngIf="!pics"></app-no-image>
    </div>
    <div>
    </div>
    <router-outlet></router-outlet>
    <app-planet-view class="image" [pics]="pics"></app-planet-view>    
</div>
<app-loader></app-loader>

// planet-detail.component.html // planet-detail.component.html

<div class="ui-g">
    <app-image [picById]="picById"></app-image>
</div>

// planet-routing.module.ts // planet-routing.module.ts

const planetRoutes: Routes = [
    {
        path: '',
        component: PlanetDataComponent,
        children: [
            {
                path: ':id',
                component: PlanetDetailComponent,
                canActivate: [AuthGuardService] },
        ],
        canActivate: [AuthGuardService] },
    ];
@NgModule({
    imports: [
        RouterModule.forChild(planetRoutes)
    ],
    exports: [RouterModule],
})
export class PlanetRoutingModule {}

// app.routing-module.ts // app.routing-module.ts

const routes: Routes = [
  { path: 'rovers', loadChildren: './planet/planet.module#PlanetModule' },
  { path: '', pathMatch: 'full', redirectTo: '/home' }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

// app.component.html // app.component.html

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

Well, with some help I could fix this issue generating another component and declaring it inside the children route. 好吧,有了一些帮助,我可以解决此问题,生成另一个组件并将其在子级路由中声明。 The parent route and the first children route are both '', so inside the parent route I just putted router-outlet wich is the one who is gonna display the child routes. 父路径和第一个子路径均为'',因此我在父路径内部放了router-outlet的那条路径将显示子路径。 The first child route It's gonna display the presentional data. 第一条子路线将显示演示数据。

// planet-routing.module.ts // planet-routing.module.ts

const planetRoutes: Routes = [
    {
        path: '',
        component: PlanetComponent, -> Component generated to display child route
        canActivate: [AuthGuardService],
        children: [
            {
                path: '',
                component: PlanetDataComponent
            },
            {
                path: ':id',
                component: PlanetDetailComponent
            }
        ]
    }
];

// planet.component.html // planet.component.html

<router-outlet></router-outlet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM