简体   繁体   English

带有辅助路线的Angular 4功能模块

[英]Angular 4 features module with auxiliary routes

I have been staring at this for a few hours now and I cannot seem to understand what I am doing wrong. 我已经盯着这个看了几个小时,我似乎无法理解我做错了什么。 Here is my scenario: I have an Angular 4 application that has a main module and an admin module, which I am loading lazily. 这是我的情况:我有一个Angular 4应用程序,该应用程序有一个主模块和一个管理模块,它们正在延迟加载。 I would like to define another router in the template of the Admin component where I can load all the auxiliary routes for the admin functionality. 我想在Admin组件的模板中定义另一个路由器,在其中可以加载所有用于admin功能的辅助路由。

Here is my app.module route configuration: 这是我的app.module路由配置:

const routes: Routes = [
    { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
    { path: 'home', component: HomeComponent },
    {
        path: 'dashboard', component: DashboardComponent, canActivate: [CanActivateGuard]
    },
    { path: 'admin', loadChildren: '/app/components/admin/admin.module#AdminModule', canActivate: [AdminGuard] },
    { path: '**', redirectTo: 'dashboard' }
];

and the admin.module: 和admin.module:

const routes: Routes = [
    {
        path: '', component: AdminDashboardComponent, children: [
            { path: 'employees', component: EmployeeManagerComponent, outlet: 'admin' }
        ]
    }
]

In the adminDashboard.html I have defined the named router as: 在adminDashboard.html中,我将命名路由器定义为:

<router-outlet name="admin"></router-outlet>

and I also have a link defined as: 而且我还有一个链接定义为:

<a [routerLink]="[{ outlets: { 'admin': ['employees']}}]">Employee Manager</a>

When I navigate to the admin dashboard, the module is loaded and the dashboard is displayed. 当我导航到管理仪表盘时,模块将加载并显示仪表盘。 When I click on the link to load the EmployeeManagerComponent into the named router nothing happens. 当我单击链接以将EmployeeManagerComponent加载到命名路由器中时,什么都没有发生。 The url changes to localhost:5000/admin/(admin:employees), but the EmployeeManagerComponent is not displayed. URL更改为localhost:5000 / admin /(admin:employees),但不显示EmployeeManagerComponent。 No errors are recorded in the console. 控制台中未记录任何错误。

I am missing something, but I cannot figure out what. 我错过了一些东西,但我不知道是什么。

Thank you for your help. 谢谢您的帮助。

So, after some trial and error, it turns out that I did not need to name the router-outlet in the lazily loaded adminDashboard component. 因此,经过一番尝试和错误之后,事实证明,我不需要在延迟加载的adminDashboard组件中命名路由器出口。 It appears that Angular is smart enough to figure out that the child routes in the module will be placed in the outlet that is closest to the main component in the module. 看来Angular足够聪明,可以确定模块中的子路线将放置在最靠近模块主要组件的插座中。

So, just removing the name and setting the links as 因此,只需删除名称并将链接设置为

<a [routerLink]="['employees']">Employees</a>

did the trick. 做到了。

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

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