Search code examples
angularangular2-componentsangular2-modules

How to access a component from different module in angular 2


I am new to angular 2, so please excuse me if this question sounds trivial to you. I am creating a feature module in angular 2 and I will export all the components from this module. Main module can import it and add this module in import list. By doing so, all the "template" in main module can access feature module's component.

But what I want is: in one of the my main module's component, I want to refer feature module's component as ViewChild.


Solution

  • You need to import the component class with a TypeScript import like

    import {MyComponent} from './my.component'
    

    then you can use it in @ViewChild()

    @ViewChild(MyComponent) myComponent:MyComponent;