【问题标题】:Error in lazy load module in angular 4.xAngular 4.x 中的延迟加载模块出错
【发布时间】:2017-06-23 12:23:40
【问题描述】:

我在 Angular 4.x 应用程序中实现了延迟加载模块。

app.route.ts

const routes: Routes = [
  {
    path: '',redirectTo:'home',pathMatch:'full'
  },
  {
    path:'home',
    children:[
      {path:'',redirectTo:'index',pathMatch:'full'},
      {path:'index',component:HomeComponent},
      {path:'dashboard',component:DashBoardComponent}
    ]
  },
  {path:'pages',
   loadChildren:'./form/form.module#FormModule'
},
   {path:'buttons',component:ButtonsComponent},

   {path:'card',component:CardComponent},
   {path:'**',component:NotFoundComponent}
];

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

form.routing.ts

const routes: Routes = [
  {
      path:'',component:FormComponent,children:[
          {path:'',redirectTo:'login',pathMatch:'full'},
        {
            path:'signup',component:RegisterComponent
        },
  {
      path:'login',component:LoginComponent},
  ]
  },

];
export const FormRouting: ModuleWithProviders = RouterModule.forChild(routes);

Form.module.ts

@NgModule({
    imports:[
        CommonModule,
        FormRouting,
        ],
    declarations:[
        FormComponent,
        LoginComponent,
        RegisterComponent
    ]

})
export class FormModule{}

应用程序在没有延迟加载的情况下工作,但在延迟加载后它会生成模板解析错误:

我在 app.module.ts 中导入了MaterialModule。我该如何解决这个问题?提前致谢。

【问题讨论】:

    标签: angular lazy-loading angular2-modules


    【解决方案1】:

    如果FormModule 中声明的组件使用MaterialModule,您还需要将MaterialModule 导入到延迟加载的FormModule

    @NgModule({
        imports: [
            CommonModule,
            FormRouting,
            MaterialModule
            ],
        declarations: [
            FormComponent,
            LoginComponent,
            RegisterComponent
        ]
    
    })
    export class FormModule{}
    

    【讨论】:

    • 仍然收到此错误Can't bind to 'disabled' since it isn't a known property of 'md-input-container
    • @GhanshyamSingh 好吧,这完全是另一个错误,你应该查看 Material Angular 文档来解决那个问题,我个人没有使用过。老实说,很明显错误是什么,您在 md-input-container 上使用了 disabled 属性,但它不存在。
    • 我必须在每个延迟加载模块中导入模块吗?有没有办法只导入一次?
    • @GhanshyamSingh 您可以创建共享模块并导出您经常使用的模块,然后只在延迟加载的模块中导入共享模块,而不是单独导入每个模块。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-14
    • 2018-04-27
    • 2018-05-22
    相关资源
    最近更新 更多