遵循Angular官方教程,懒惰模块Angular 8不起作用

时间:2019-07-13 17:14:36

标签: angular

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

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>

1 个答案:

答案 0 :(得分:0)

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

要么:

(a == b && b == c)

或:

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