本文介绍了Angular 5组件不是任何NgModule的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a new component called "player-create" and added it to the declariations in app.module. However when I try to load a page it gives the following error:

Component PlayerCreateComponent不是任何NgModule的一部分,或者模块尚未导入到您的模块中.

Component PlayerCreateComponent is not part of any NgModule or the module has not been imported into your module.

PlayerCreateModule.ts

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

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 { }

推荐答案

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

Since you declared your component in another module, you need to add PlayerCreateComponent to the exports of your PlayerCreateModule

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

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

and in your main module, import PlayerCreateModule (not just the component)

//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 { }

这篇关于Angular 5组件不是任何NgModule的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 03:11