Angular 5组件不是任何NgModule的一部分

时间:2018-02-14 19:01:53

标签: angular

我创建了一个名为" player-create"的新组件。并将其添加到app.module中的声明中。但是,当我尝试加载页面时,它会出现以下错误:

组件PlayerCreateComponent不是任何NgModule的一部分,或者该模块尚未导入您的模块。

PlayerCreateModule.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {PlayerCreateComponent} from './player-create.component';


@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [
    PlayerCreateComponent
  ]
})
export class PlayerCreateModule { }

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppRoutingModule } from './app-routing.module';
import { SharedModule } from "./shared/shared.module";
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

import { AppComponent } from './app.component';
import { ContentLayoutComponent } from "./layouts/content/content-layout.component";
import { FullLayoutComponent } from "./layouts/full/full-layout.component";

import { AuthService } from './shared/auth/auth.service';
import { AuthGuard } from './shared/auth/auth-guard.service';

import * as $ from 'jquery';
import {HomeLayoutComponent} from './layouts/home/home-layout.component';
import {PlayersLayoutComponent} from './layouts/players/players-layout.component';
import {HttpClientModule} from '@angular/common/http';
import {ModalModule} from 'ngx-bootstrap';
import { PlayerCreateComponent } from './pages/player-create/player-create.component';
import { PlayerEditComponent } from './pages/player-edit/player-edit.component';

@NgModule({
    declarations: [
        AppComponent,
        FullLayoutComponent,
        ContentLayoutComponent,
        HomeLayoutComponent,
        PlayersLayoutComponent,
        PlayerCreateComponent
    ],
    imports: [
        BrowserAnimationsModule,
        AppRoutingModule,
        SharedModule,
        HttpClientModule,
        ModalModule.forRoot(),
        NgbModule.forRoot()
    ],
    providers: [
        AuthService,
        AuthGuard
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

3 个答案:

答案 0 :(得分:4)

由于您在另一个模块中声明了组件,因此需要将PlayerCreateComponent添加到PlayerCreateModule的导出

//PlayerCreateModule.ts 
@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [
    PlayerCreateComponent
  ],
  exports: [
    PlayerCreateComponent
  ]
})
export class PlayerCreateModule { }    

并在主模块中导入PlayerCreateModule(不仅仅是组件)

//app.module.ts
@NgModule({
    declarations: [
        AppComponent,
        FullLayoutComponent,
        ContentLayoutComponent,
        HomeLayoutComponent,
        PlayersLayoutComponent,
        //PlayerCreateComponent <== not needed if it's declared in another module
    ],
    imports: [
        BrowserAnimationsModule,
        AppRoutingModule,
        SharedModule,
        HttpClientModule,
        ModalModule.forRoot(),
        NgbModule.forRoot(),
        PlayerCreateModule //<==== add that
    ],
    providers: [
        AuthService,
        AuthGuard
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

答案 1 :(得分:1)

您需要在app.module.ts中的导入下添加 PlayerCreateModule ,无需在app.module.ts中再次添加组件

  imports: [
    PlayerCreateModule
  ]

答案 2 :(得分:-1)

你需要添加

  

entryComponents

在你的app模块中。

Eample:

entryComponents: [' Your App Components Name']