Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Create Application-Specific Angular 2 Components

    2 - 6

    Many components you create will be specific to your application because they will rely on your application services. These application-specific components are often called “feature” components and they follow a module pattern which integrates them with your application.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    A feature component usually lives inside of its own directory with its own module. I'll create a directory called home, and say home.component.ts. Inside of this file, we'll create a component. This will have a selector of home, and a template of a div saying, "I'm a home component."

    Just make sure to decorate this as a class. Then our future component is basically done. It's just some configuration inside of this home directory, where we create a home.module.ts. Inside of here, we'll say NG module.

    Our declarations, we need to declare the home component. As well this time, we also need to export the home component. Now we export the class home module. The reason you're declaring this twice inside of exports and inside of declarations is because you could be importing this home component from somewhere else.

    You either declare it or import it inside of the imports where you import other modules. Because this is living inside of this module, we just declare it, and then re-export it. Now inside of our app module, we can import the home module.

    Inside of our app component, we now have access to the home component. This home matches the home component selector. You can see between these two components, home and app component, in app we're just using that home selector, in home, now we've created that selector, and created a template for it.

    I'll hit save. You'll now see I'm a home component. Again, the full div structure looks like this, where you have your main, which is bootstrapping the module. The module is importing both the browser module and the home module.

    In the app component, you're referencing this home selector, which is available because in app module, you're importing the home module. This home selector is available because in app module, you are importing home module.

    Everything in home module is available to this, but the only thing we're exporting for now is just the home component. Home component has a selector of home, and a template of, "I am a home component," which lands here as, "I'm a home component."