简体   繁体   English

如何在Angular中创建嵌套路由设置

[英]How to create a nested routing setup in Angular

I am trying to create a nested router setup in angular 我正在尝试以角度创建嵌套路由器设置

App > Core > General App> Core> General

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

const routes:Routes = [
    {path: 'core', loadChildren: './core/core.module#CoreModule', canActivate: [AuthGuard]}
];

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

core-routing.module.ts 核心routing.module.ts

const coreRoutes:Routes = [
    {
        path: '',
        component: CoreComponent,
        children: [
            { path: 'general', loadChildren: './general/general.module#GeneralModule' },
        ]
    }
];

@NgModule({
    imports: [RouterModule.forChild(coreRoutes)],
    exports: [RouterModule]
})
export class CoreRoutingModule {
}

general-routing.module.ts 一般routing.module.ts

const routes: Routes = [
  {
    path: '',
    component: GeneralComponent,
    children: [
        { path: 'vendor/browser', component: VendorBrowserComponent },
    ]
  }
];

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

The issue I am observing is that Angular is unable to load directive in my nested template. 我观察到的问题是Angular无法在我的嵌套模板中加载指令。

I am getting this error in the vendor template: 我在供应商模板中收到此错误:

ERROR Error: Uncaught (in promise): Error: Template parse errors:
Can't bind to 'ngIf' since it isn't a known property of 'div'. ("        </div>
          <div class="ui icon right-button">
              <div class="inline field" [ERROR ->]*ngIf="editPermission">
                  <ng-container >
                      <div class="ui toggle"): ng:///GeneralModule/VendorBrowserComponent.html@10:40
Property binding ngIf not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". ("
          </div>
          <div class="ui icon right-button">
              [ERROR ->]<div class="inline field" *ngIf="editPermission">
                  <ng-container >
                 "): 

The solution is to import CommonModule in each of your modules. 解决方案是在每个模块中导入CommonModule

import { CommonModule } from '@angular/common'; 

@NgModule({
  imports: [CommonModule],
  declarations: [MyComponent]
  ...
})
class MyComponentModule {}

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

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