This tutorial lesson demonstrates how to add the HousingLocation component to your Angular app.
What you'll learn
- Your app has a new component:
HousingLocationand it displays a message confirming that the component was added to your application.
-
Create the
HousingLocationIn 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
HousingLocationng generate component housingLocationRun this command to build and serve your app.
ng serveNOTE: This step is only for your local environment!
Open a browser and navigate to
http://localhost:4200to find the application.Confirm that the app builds without error.
HELPFUL: 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.
-
Add the new component to your app's layout
In this step, you add the new component,
HousingLocationto your app'sHome, so that it displays in your app's layout.In the Edit pane of your IDE:
Open
home.tsin the editor.In
home.ts, importHousingLocationby adding this line to the file level imports.Import HousingLocation in src/app/home/home.ts
import {Component} from '@angular/core';import {CommonModule} from '@angular/common';import {HousingLocation} from '../housing-location/housing-location';@Component({ selector: 'app-home', imports: [CommonModule, HousingLocation], template: ` <section> <form> <input type="text" placeholder="Filter by city" /> <button class="primary" type="button">Search</button> </form> </section> <section class="results"> <app-housing-location></app-housing-location> </section> `, styleUrls: ['./home.css'],})export class Home {}Next update the
importsproperty of the@Componentmetadata by addingHousingLocationto the array.Add HousingLocation to imports array in src/app/home/home.ts
import {Component} from '@angular/core';import {CommonModule} from '@angular/common';import {HousingLocation} from '../housing-location/housing-location';@Component({ selector: 'app-home', imports: [CommonModule, HousingLocation], template: ` <section> <form> <input type="text" placeholder="Filter by city" /> <button class="primary" type="button">Search</button> </form> </section> <section class="results"> <app-housing-location></app-housing-location> </section> `, styleUrls: ['./home.css'],})export class Home {}Now the component is ready for use in the template for the
Home. Update thetemplateproperty of the@Componentmetadata to include a reference to the<app-housing-location>tag.Add housing location to the component template in src/app/home/home.ts
import {Component} from '@angular/core';import {CommonModule} from '@angular/common';import {HousingLocation} from '../housing-location/housing-location';@Component({ selector: 'app-home', imports: [CommonModule, HousingLocation], template: ` <section> <form> <input type="text" placeholder="Filter by city" /> <button class="primary" type="button">Search</button> </form> </section> <section class="results"> <app-housing-location></app-housing-location> </section> `, styleUrls: ['./home.css'],})export class Home {}
-
Add the styles for the component
In this step, you will copy over the pre-written styles for the
HousingLocationto your app so that the app renders properly.Open
src/app/housing-location/housing-location.css, and paste the styles below into the file:NOTE: In the browser, these can go in
src/app/housing-location/housing-location.tsin thestylesarray.Add CSS styles to housing location to the component in src/app/housing-location/housing-location.css
.listing { background: var(--accent-color); border-radius: 30px; padding-bottom: 30px;}.listing-heading { color: var(--primary-color); padding: 10px 20px 0 20px;}.listing-photo { height: 250px; width: 100%; object-fit: cover; border-radius: 30px 30px 0 0;}.listing-location { padding: 10px 20px 20px 20px;}.listing-location::before { content: url("/assets/location-pin.svg") / "";}section.listing a { padding-left: 20px; text-decoration: none; color: var(--primary-color);}section.listing a::after { content: "\203A"; margin-left: 5px;}Save your code, return to the browser and confirm that the app builds without error. You should find the message "housing-location works!" rendered to the screen.Correct any errors before you continue to the next step.

SUMMARY: In this lesson, you created a new component for your app and added it to the app's layout.