简体   繁体   English

带有延迟加载模块的 Angular 8 路由,子路由中的子路由可以跳过父路由来访问子路由

[英]Angular 8 Routing w/ Lazy Loading modules, child within a child route can skip parent route to access child

I have the following simple structure in my application that I am trying to work with:我正在尝试使用的应用程序中有以下简单结构:

/
/home
/admin
    /setup
    /other

I have the router using modules and lazy loading.我有使用模块和延迟加载的路由器。 So the administrator page ( /admin ) of my application controls the access to the subpages for setting up the application.因此,我的应用程序的管理员页面 ( /admin ) 控制对用于设置应用程序的子页面的访问。 Eventually, the Admin route ( /admin ) will be route guarded, but right now I am just building the empty templates and some simple tests for the routing.最终,管理路由 ( /admin ) 将受到路由保护,但现在我只是构建空模板和一些简单的路由测试。 This is all working.这一切都在起作用。 Bad addresses are captured, missing addresses routing to / , etc.错误地址被捕获,丢失地址路由到/等。

My problem is in protecting my addressing so that people cannot bypass the true routes of the application and get to pages they should not see.我的问题是保护我的地址,以便人们无法绕过应用程序的真实路由并访问他们不应该看到的页面。 Go to /setup instead of /admin/setup .转到/setup而不是/admin/setup

Trying to keep things self contained, I did not want to rely on route guards on all the submodules, just the routing under a page that is guarded.为了让事情自成一体,我不想依赖所有子模块上的路由守卫,只依赖受守卫的页面下的路由。 This way, new modules loaded under the admin page will automatically be protected, in case a developer forgets to add the guard.这样,在管理页面下加载的新模块将自动受到保护,以防开发人员忘记添加保护。 Also, the individual feature modules have their own routing, so this is not in one central location as most examples use.此外,各个功能模块都有自己的路由,因此这并不像大多数示例使用的那样位于一个中心位置。

The code works properly going to /admin/setup, /admin/set will go to the / root.代码在 /admin/setup 中正常工作,/admin/set 将进入 /root。 The problem I am having is that /setup also goes to the lazy loaded module for the Setup, which is a child of the Admin module routing.我遇到的问题是 /setup 也转到安装程序的延迟加载模块,它是管理模块路由的子项。

EDIT: Update the routing modules based on suggestions, but the issue is still present.编辑:根据建议更新路由模块,但问题仍然存在。

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

const routes: Routes = [
    { path: '', component: DashboardComponent },
    { path: 'home', component: HomeComponent },
    { path: 'admin', loadChildren: () => import('./administrator/administrator.module').then(m => m.AdministratorModule) },

    { path: '**', redirectTo: '/' }     // Wrong route entered, return to main route
];

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

admin-routing.module.ts admin-routing.module.ts

const routes: Routes = [
    { path: '', component: AdministratorComponent,
        children: [
            { path: 'setup', loadChildren: () => import('./setup/setup.module').then(m => m.SetupModule) },
            { path: 'zzz', loadChildren: () => import('./zzz/zzz.module').then(m => m.zzzModule) }
        ]
    }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
})

EDIT #2: Add example import of the Administrator routing in admin.module.ts.编辑 #2:在 admin.module.ts 中添加管理员路由的示例导入。

admin.module.ts管理模块.ts

@NgModule({
    declarations: [AdministratorComponent],
    imports: [
        CommonModule,
        MaterialModule,
        SetupModule,
        zzzModule,
        AdministratorRoutingModule
    ]
})
export class AdministratorModule { }

The problem is that I can go through properly to /admin/setup.问题是我可以正确地进入/admin/setup。 However, you can also get to this page by simply going to /setup.但是,您也可以通过简单地转到 /setup 来访问此页面。 I do not want someone to be able to bypass the routing and get to restricted pages.我不希望有人能够绕过路由并访问受限页面。

I am not sure how to protect against this route ( /setup ) working.我不确定如何防止这条路线 ( /setup ) 工作。

Hello I'm not sure I understand well your problem and I don't understand your admin routes too.您好,我不确定我是否了解您的问题,我也不了解您的管理路线。

If I read your routes, I expect you have this routes:如果我阅读了您的路线,我希望您有以下路线:

  • / /
  • /home /家
  • /admin/admin (because you have admin in parent and admin in child) /admin/admin (因为你在父级有 admin,在子级有 admin)
  • /admin/setup /管理员/设置
  • /admin/zzz /管理员/zzz

But I don't understand the './' before your routes.但我不明白你的路线前的“./”。 It can be the problem.这可能是问题所在。

Did you try something like that ?你有没有尝试过这样的事情? (This is valid if you have a router-outlet into your AdministratorComponent too) (如果您的 AdministratorComponent 也有一个路由器插座,这也是有效的)

admin-routing.module.ts admin-routing.module.ts

const routes: Routes = [
    {
        path: '',
        component: AdministratorComponent,
        children: [
            { path: 'setup', loadChildren: () => import('./setup/setup.module').then(m => m.SetupModule) }
            { path: 'zzz', loadChildren: () => import('./zzz/zzz.module').then(m => m.zzzModule) }
        ]
    }
]; 

admin.module.ts管理模块.ts

@NgModule({
    declarations: [AdministratorComponent],
    imports: [
        CommonModule,
        MaterialModule
        AdministratorRoutingModule
    ]
})
export class AdministratorModule { }

If you import modules here, you will note use lazy-load and you will add routes in current module.如果你在这里导入模块,你会注意到使用延迟加载,你会在当前模块中添加路由。

This is how I would have done it这就是我要做的

const routes: Routes = [
    { path: '', component: AdministratorComponent },
    { path: 'setup', loadChildren: () => import('./setup/setup.module').then(m => m.SetupModule) }
    { path: 'zzz', loadChildren: () => import('./zzz/zzz.module').then(m => m.zzzModule) }
];

@NgModule({
    imports: [RouterModule.forChild(routes)],
    exports: [RouterModule]
})

which would not let user to navigate to only: /setup .这不会让用户只导航到: /setup Only admin/setup would exist.只有admin/setup会存在。 And no more /admin/admin没有更多的/admin/admin

And within the lazy loaded modules setup.module and zzz.module.在延迟加载模块 setup.module 和 zzz.module 中。 Make the same changes to any route there对那里的任何路线进行相同的更改

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

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