Angular 2 'component' is not a known element

后端 未结 15 2090
傲寒
傲寒 2020-11-30 17:58

I\'m trying to use a component I created inside the AppModule in other modules. I get the following error though:

\"Uncaught (in promise): Error: Temp

15条回答
  •  Happy的楠姐
    2020-11-30 18:43

    This convoluted framework is driving me nuts. Given that you defined the custom component in the the template of another component part of the SAME module, then you do not need to use exports in the module (e.g. app.module.ts). You simply need to specify the declaration in the @NgModule directive of the aforementioned module:

    // app.module.ts
    
    import { JsonInputComponent } from './json-input/json-input.component';
    
    @NgModule({
      declarations: [
        AppComponent,
        JsonInputComponent
      ],
      ...
    

    You do NOT need to import the JsonInputComponent (in this example) into AppComponent (in this example) to use the JsonInputComponent custom component in AppComponent template. You simply need to prefix the custom component with the module name of which both components have been defined (e.g. app):

    Notice app-json-input not json-input!

    Demo here: https://github.com/lovefamilychildrenhappiness/AngularCustomComponentValidation

提交回复
热议问题