Each individual lesson should have the images available in the download since a learner can start from any lesson. So, we're adding images here. Also, sync'ing styles for consistency across the lessons. This is fixing some previous commits that didn't include these changes. The goal is that for future commits we can have less changes and focused solely on that lesson PR Close #49980
7 KiB
First Angular app lesson 2 - Create Home component
This tutorial lesson demonstrates how to create a new component for your Angular app.
Time required: expect to spend about 10 minutes to complete this lesson.
Before you start
This lesson starts with the code from the previous lesson, so you can:
- Use the code that you created in Lesson 1 in your integrated development environment (IDE).
- Start with the code example from the previous lesson. Choose the from Lesson 1 where you can:
- Use the live example in StackBlitz, where the StackBlitz interface is your IDE.
- Use the download example and open it in your IDE.
If you haven't reviewed the introduction, visit the Introduction to Angular tutorial to make sure you have everything you need to complete this lesson.
If you have any trouble during this lesson, you can review the completed code for this lesson, in the for this lesson.
After you finish
- Your app has a new component:
HomeComponent.
Conceptual preview of Angular components
Angular apps are built around components, which are Angular's building blocks. Components contain the code, HTML layout, and CSS style information that provide the function and appearance of an element in the app. In Angular, components can contain other components. An app's functions and appearance can divided and partitioned into components.
In Lesson 1, you updated the AppComponent, whose function is to contain all the other components.
In this lesson, you create a HomeComponent to display the home page of the app.
In later lessons, you create more components to provide more features to the app.
In Angular, components have metadata that define its properties.
When you create your HomeComponent, you use these properties:
selector: to describe how Angular refers to the component in templates.standalone: to describe whether the component requires angModule.imports: to describe the component's dependencies.template: to describe the component's HTML markup and layout.styleUrls: to list the URLs of the CSS files that the component users in an array.
Components have other properties, but these are the ones used by HomeComponent.
Lesson steps
Perform these steps on the app code in your IDE.
Step 1 - Create the HomeComponent
In this step, you create a new component for your app.
In the Terminal pane of your IDE:
-
In your project directory, navigate to the
first-appdirectory. -
Run this command to create a new
HomeComponentng generate component Home --standalone --inline-template --skip-tests
-
Run this command to build and serve your app.
ng serve
-
Open a browser and navigate to
http://localhost:4200to find the application. -
Confirm that the app builds without error.
Note: It should render the same as it did in the previous lesson because even though you added a new component, you haven't included it in any of the app's templates, yet.
-
Leave
ng serverunning as you complete the next steps.
Step 2 - Add the new component to your app's layout
In this step, you add the new component, HomeComponent to your app's root component, AppComponent, so that it displays in your app's layout.
In the Edit pane of your IDE:
-
Open
app.component.tsin the editor. -
In
app.component.ts, importHomeComponentby adding this line to the file level imports. -
In
app.component.ts, in@Component, update theimportsarray property and addHomeComponent. -
In
app.component.ts, in@Component, update thetemplateproperty to include the following HTML code. -
Save your changes to
app.component.ts. -
If
ng serveis running, the app should update. Ifng serveis not running, start it again. Hello world in your app should change to home works! from theHomeComponent. -
Check the running app in the browser and confirm that the app has been updated.
Step 3 - Add features to HomeComponent
In this step you add features to HomeComponent.
In the previous step, you added the default HomeComponent to your app's template so its default HTML appeared in the app.
In this step, you add a search filter and button that is used in a later lesson.
For now, that's all that HomeComponent has.
Note that, this step just adds the search elements to the layout without any function, yet.
In the Edit pane of your IDE:
-
In the
first-appdirectory, openhome.component.tsin the editor. -
In
app.component.ts, in@Component, update thetemplateproperty with this code. -
Next, open
home.component.cssin the editor and update the content with these styles. -
Confirm that the app builds without error. You should find the filter query box and button in your app and they should be styled. Correct any errors before you continue to the next step.
Lesson review
In this lesson, you created a new component for your app and gave it a filter edit control and button.
If you are having any trouble with this lesson, you can review the completed code for it in the .
Next steps
More information
For more information about the topics covered in this lesson, visit: