【问题标题】:Lazy Module Angular 8 doesn't work, following the Official Angular tutorial遵循官方 Angular 教程,惰性模块 Angular 8 不起作用
【发布时间】:2019-07-13 17:14:36
【问题描述】:

我完全按照官方文档中的这个教程:

https://angular.io/guide/lazy-loading-ngmodules

允许我们在 Angular 8 中创建延迟加载的模块

我有一个我无法弄清楚的错误,我在互联网上没有找到任何类似的问题..

我有这个错误:

ERROR Error: Uncaught (in promise): Error: No NgModule metadata found for 'undefined'.
Error: No NgModule metadata found for 'undefined'.
    at NgModuleResolver.resolve (compiler.js:20665)
    at CompileMetadataResolver.getNgModuleMetadata (compiler.js:19794)
    at JitCompiler._loadModules (compiler.js:25582)
    at JitCompiler._compileModuleAndComponents (compiler.js:25565)
    at JitCompiler.compileModuleAsync (compiler.js:25527)
    at CompilerImpl.compileModuleAsync (platform-browser-dynamic.js:216)
    at MergeMapSubscriber.project (router.js:5391)
    at MergeMapSubscriber._tryNext (mergeMap.js:46)
    at MergeMapSubscriber._next (mergeMap.js:36)
    at MergeMapSubscriber.next (Subscriber.js:49)
    at resolvePromise (zone-evergreen.js:797)
    at resolvePromise (zone-evergreen.js:754)
    at zone-evergreen.js:858
    at ZoneDelegate.invokeTask (zone-evergreen.js:391)
    at Object.onInvokeTask (core.js:34182)
    at ZoneDelegate.invokeTask (zone-evergreen.js:390)
    at Zone.runTask (zone-evergreen.js:168)
    at drainMicroTaskQueue (zone-evergreen.js:559)

此错误出现在按钮单击时的浏览器控制台中。

如果你已经遇到过这个问题,请帮助我...我卡住了...

更新:

在 app-routing.module.ts 中

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';


const routes: Routes = [
  {
    path: 'astreintes',
    loadChildren : () => import('./astreintes/astreintes.module').then(module => {module.AstreintesModule})
  },
  {
    path: 'profile',
    loadChildren : () => import('./profile/profile.module').then(module => {module.ProfileModule})
  },
  {
    path: '',
    redirectTo: '',
    pathMatch: 'full'
  }
];

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

在 profile-routing.module.ts 中

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { UserProfileComponent } from './user-profile/user-profile.component';


const routes: Routes = [
  {
    path : '',
    component : UserProfileComponent
  }
];

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

在 astreinte-routing.module.ts 中(Astreinte 是一个法语单词):

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AstreintesListComponent } from './astreintes-list/astreintes-list.component';


const routes: Routes = [
  {
    path: '',
    component: AstreintesListComponent
  }
];

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

我的组件只显示

<p>profile works!</p>

<p>astreintes-list works!</p>

【问题讨论】:

  • 请提供您的代码 ;-)
  • No NgModule 表示没有模块或模块中有问题。最好发布您的路由和模块代码。
  • @Mahi 我已经更新了我的帖子 :),它是一个非常简单的应用程序,它只会尝试使工作延迟加载模块.....
  • 请显示您注册组件的模块文件。和 main.ts

标签: angular


【解决方案1】:

您的 lambda 函数不返回任何内容。删除括号或添加返回语句。

要么:

loadChildren : () => import('./profile/profile.module').then(module => {return module.ProfileModule})

或:

loadChildren : () => import('./profile/profile.module').then(module => module.ProfileModule)

【讨论】:

    猜你喜欢
    • 2018-11-30
    • 2012-09-24
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    相关资源
    最近更新 更多