找不到“ {模块名称}”的NgModule元数据中的错误

时间:2018-11-10 17:15:35

标签: angular webpack

我正在使用以下版本的CLI:

ngular CLI: 6.2.7
Node: 10.6.0
OS: darwin x64
Angular: 6.1.10
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.8.7
@angular-devkit/build-angular     0.8.7
@angular-devkit/build-optimizer   0.8.7
@angular-devkit/build-webpack     0.8.7
@angular-devkit/core              0.8.7
@angular-devkit/schematics        0.8.7
@angular/cli                      6.2.7
@ngtools/webpack                  6.2.7
@schematics/angular               0.8.7
@schematics/update                0.8.7
rxjs                              6.2.2
typescript                        2.9.2
webpack                           4.16.4

我创建了一个名为char-routing.module.ts的子ng模块,当我尝试创建类时,其抛出错误当我运行ng服务时,抛出了以下错误:

ERROR in No NgModule metadata found for 'CharRoutingModule'.

我正在使用NgModule loadChildren: './add-character/add-character.component#CharRoutingModule'的延迟加载 我要添加两个文件的来源。我无法复制为什么会这样的问题,我是新手,所以,我对此没有更多的想法。 char-routing.module.ts

的代码
import { NgModule } from '@angular/core';
import { AddCharacterComponent } from './add-character.component';
import { RouterModule } from '@angular/router';

@NgModule({
  imports: [RouterModule.forChild([
    {path: '', component: AddCharacterComponent}
  ])],
  declarations: [AddCharacterComponent]
})
    export class CharRoutingModule {

}

add-character.component.ts的代码

import { StarWarService } from './../star-war.service';
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-add-character',
  templateUrl: './add-character.component.html',
  styleUrls: ['./add-character.component.css']
})
export class AddCharacterComponent implements OnInit {
  ablavelSide: {display: string, value: string}[] = [
    {display: 'None', value: ''},
    {display: 'Light', value: 'light'},
    {display: 'Dark', value: 'dark'}
  ];
  swService: StarWarService;
  constructor(serive: StarWarService) {
    this.swService = serive;
  }

  ngOnInit() {
  }

  onSubmit (form) {
    console.log(form);
    if (form.invalid) {
      return;
    }
    this.swService.addCharracter(form.value.name, form.value.side);
    form.reset();
  }
}

这是我正在使用的代码。请帮助我。

1 个答案:

答案 0 :(得分:3)

您为CharRoutingModule文件提供了错误的路径。

loadChildren: './add-character/add-character.component#CharRoutingModule'

应该是

loadChildren: './add-character/char-routing.module#CharRoutingModule'

但是理想情况下,应该在loadChildren中引用一个CharModule,然后在其中包含CharRoutingModule的路由配置的CharModule