官术网_书友最值得收藏!

Adding a new module

In this example, we will, so that you can see how to build applications using the Angular CLI. Even in this very basic example, we will cover the following points:

  • How to organize an Angular application
  • Creating modules
  • Creating services
  • Template data binding
  • Running an application in production

Now, let's create a module that shows us a list of beers:

  1. Open VS Code, and, inside the integrated Terminal, type the following command:
ng g module beers

Note that the command ng g module is a shortcut to ng generate module <module-name>, and this command just creates the module; we need to add routes, components, and templates, and also import the beers module in app.modules.ts, at the root of the app folder. The preceding command will generate the following structure and file content inside of our project: src/app/beers/beers.module.ts. The beers.module.ts contents are as follows:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
imports: [
CommonModule
],
declarations: []
})
export class BeersModule { }

This is a pretty simple boilerplate code, but it is very useful. Now, we will add the missing pieces.

  1. Add the beers module to your app module; open app.module.ts and replace the code with the following lines:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BeersModule } from './beers/beers.module';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule,
BeersModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

Note that we imported BeersModule and added it to the imports array.

主站蜘蛛池模板: 新郑市| 嘉荫县| 五常市| 海晏县| 永昌县| 西畴县| 黄梅县| 灵寿县| 昂仁县| 固原市| 五家渠市| 修文县| 萝北县| 炎陵县| 凤凰县| 乌拉特中旗| 千阳县| 永城市| 博兴县| 漯河市| 周口市| 台南市| 德格县| 抚顺市| 毕节市| 平昌县| 台北市| 永嘉县| 达孜县| 崇明县| 慈溪市| 绥芬河市| 周至县| 石阡县| 贵溪市| 铅山县| 九寨沟县| 林口县| 靖宇县| 白朗县| 化隆|