【问题标题】:Uncaught Error: Unexpected directive 'NavComponent' imported by the module 'AppModule'. Please add a @NgModule annotation未捕获的错误:模块“AppModule”导入的意外指令“NavComponent”。请添加@NgModule 注释
【发布时间】:2020-05-30 23:50:45
【问题描述】:

我有一个自定义组件(导航)里面。

当我使用它时,webpack编译成功结束,但chrome抛出以下错误:

未捕获的错误:模块“AppModule”导入了意外的指令“NavComponent”。请添加@NgModule 注解。

这是我的 AppModule:

>

 import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { NavComponent } from './nav/nav.component';

@NgModule({
  declarations: [AppComponent ],
  entryComponents: [],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule, NavComponent],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

【问题讨论】:

    标签: android angular ionic-framework ionic4


    【解决方案1】:

    如果您的NavComponent@Component@Directive,则需要将其添加到declarations,而不是imports

    ...
    declarations: [ AppComponent, NavComponent ],
    ...
    

    【讨论】:

    • 我改了,但我现在有这个问题。
    • ERROR 错误:未捕获(在承诺中):错误:模板解析错误:'app-nav' 不是已知元素:
    • @MedYassineMessaoud 取决于你在哪里使用它
    • 我在另一个组件中使用它,比如在 angular 中,但我不明白这个问题
    猜你喜欢
    • 2019-04-22
    • 2018-01-31
    • 2017-12-02
    • 2017-11-27
    • 2019-03-08
    • 2018-06-06
    • 1970-01-01
    • 2018-01-10
    • 2019-12-01
    相关资源
    最近更新 更多