Angular 2 'component' is not a known element

后端 未结 15 2117
傲寒
傲寒 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条回答
  •  无人及你
    2020-11-30 18:58

    These are the 5 steps I perform when I got such an error.

    • Are you sure the name is correct? (also check the selector defined in the component)
    • Declare the component in a module?
    • If it is in another module, export the component?
    • If it is in another module, import that module?
    • Restart the cli?

    I also tried putting ContactBoxComponent in CustomersAddComponent and then in another one (from different module) but I got an error saying there are multiple declarations.

    You can't declare a component twice. You should declare and export your component in a new separate module. Next you should import this new module in every module you want to use your component.

    It is hard to tell when you should create new module and when you shouldn't. I usually create a new module for every component I reuse. When I have some components that I use almost everywhere I put them in a single module. When I have a component that I don't reuse I won't create a separate module until I need it somewhere else.

    Though it might be tempting to put all you components in a single module, this is bad for the performance. While developing, a module has to recompile every time changes are made. The bigger the module (more components) the more time it takes. Making a small change to big module takes more time than making a small change in a small module.

提交回复
热议问题