Next, follow these steps to implement the products-page component with just the menu for now:
- Generate the component using Angular CLI by running the following:
ng g component modules/market/products-page
- Replace the HTML file /src/app/modules/market/products-page/products-page.component.html contents with the following:
<app-category-menu></app-category-menu>
- Add ProductsPageComponent to the market module's exports as follows:
- Open the market.module.ts file.
- Add exports to the @NgModule decorator, with an array as the value.
- Add ProductsPageComponent to the exports array.
@NgModule({
imports: [
CommonModule
],
declarations: [
CategoryMenuItemComponent,
CategoryMenuComponent,
ProductsPageComponent,
],
exports: [
ProductsPageComponent,
]
})
export class MarketModule { }
Finally, add the products page to the root app component as follows:
- Add the products page component to app.component.html:
<app-header></app-header>
<div class="main-area">
<app-products-page></app-products-page>
</div>
- Replace the CSS file contents in the app.component.css file with the following:
.main-area {
margin: 10px;
}
- Since AppComponent uses the ProductsPageComponent, AppModule now requires MarketModule: so let's add it as follows:
- Add MarketModule to the imports of AppModule by adding the following to app.module.ts:
import { MarketModule } from './modules/market/market.module';
@NgModule({
declarations: [
AppComponent,
HeaderComponent
],
imports: [
BrowserModule,
SharedModule,
MarketModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
You can now run the application and see the category menu items in place. It should look similar to that in the following screenshot, only with different categories:
Currently, the menu items are defined statically with a constant name.
To complete the functionality of the category menu items, we need to do the following:
- CategoryMenuComponent should receive the menu items data from its ProductsPageComponent parent component.
- CategoryMenuItemComponent should receive the item name, and an indication of whether or not it is selected from its CategoryMenuComponent parent component.
- CategoryMenuComponent should manage the menu item selection by doing the following:
- CategoryMenuItemComponent should notify it when it is clicked.
- It should maintain the selection as part of its state.
- It should notify selection changes to its parent.
The following diagram visualizes the preceding detailed steps:
For the purpose of implementing the preceding flow, you will learn about data binding and component interaction next.