【发布时间】:2018-02-06 10:05:17
【问题描述】:
我正在开发一个显示表单和用户列表的应用程序。 我试图通过 2 个按钮延迟加载这两个模块,但组件不加载。 我可以在我的谷歌开发工具的网络选项卡中看到 form.module.chunk.js 和 people.module.chunk.js,因此正在加载模块。 如果这可能是问题,我在 /form 或 /people 中没有任何子文件夹
app-routing-module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { MenuComponent } from './menu/menu.component';
const routes: Routes = [
{
path: 'people',
loadChildren: 'app/people/people.module#PeopleModule'
},
{
path: 'form',
loadChildren: 'app/form/form.module#FormModule'
},
{ path: 'menu', component: MenuComponent },
{ path: '', redirectTo: '/menu', pathMatch: 'full' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
然后在模块本身中我声明了组件
declarations: [PeopleComponent]
最后是路由模块的代码
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { PeopleComponent } from './people.component';
const routes: Routes = [
{
path: '',
component: PeopleComponent
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class PeopleRoutingModule { }
如果有任何建议,我都会很高兴
【问题讨论】:
-
点击人员路线时控制台出现什么错误,这是我关于延迟加载的答案,请看一下,看看你在做什么stackoverflow.com/questions/48554772/…
-
@Rakeschand Uncaught (in promise):错误:FormComponent 类型是 2 个模块声明的一部分:AppModule 和 FormModule!请考虑将 FormComponent 移至导入 AppModule 和 FormModule 的更高模块。您还可以创建一个新的 NgModule,它导出并包含 FormComponent,然后在 AppModule 和 FormModule 中导入该 NgModule。错误:FormComponent 类型是 2 个模块声明的一部分:AppModule 和 FormModule!请考虑将 FormComponent 移至导入 AppModule 和 FormModule 的更高模块。
-
@Rakeschand 我是否必须将组件放入单独的文件夹中?
-
没错,你的懒加载组件应该只在懒加载模块中声明。但是如果您需要在多个地方导入一个组件,则有不同的方法
-
不是 Jeremy,不是文件夹,不是文件夹,而是声明,
declarations: [PeopleComponent]这个PeopleComponent应该在一个模块的declarations中
标签: angular typescript lazy-loading