Angular 2 global component

user4676340 picture user4676340 · Jan 2, 2017 · Viewed 8.1k times · Source

My structure is as follows :

App
    *component, module, template*
    Component 1
        *component, module, template*
        Component 11
            *component, module, template*
    Global Component
        *component, temaplte, BUT NO MODULE*

My App module contains the component 1 and component 11 modules. I declared the global component in the app module, so that I can use it in all my other components. I also exported it in app module.

But when I use the selector in the component 11 template, the console says not a known element.

I tried importing it in my other components but it says it's already imported, and that I should import it in a higher module.

Could you tell me how to declare a component so that it can be used in child templates ?

Answer

Karl picture Karl · Jan 2, 2017

I'd assume "global components" are not known in the angular 2 architecture. See also here ==> https://angular.io/docs/ts/latest/guide/architecture.html

From experience I know that it can be a problem if components (e.g. pipes) are imported at the app.module level. When doing this with a pipe for example you get a "pipe not found" error. Maybe you encounter a similiar problem with your component.

So I'd recommend to make your component part of a module and then import your module in your app.module instead of the component.