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

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.

主站蜘蛛池模板: 启东市| 吴川市| 邓州市| 姚安县| 肃北| 普兰县| 商南县| 和静县| 湖南省| 鄢陵县| 湾仔区| 辰溪县| 上高县| 屯留县| 合肥市| 麻阳| 云梦县| 镇安县| 阿拉善右旗| 岫岩| 封开县| 芒康县| 龙游县| 定西市| 宁安市| 蓬安县| 泾源县| 罗城| 婺源县| 贵南县| 闽侯县| 梅州市| 昭觉县| 博罗县| 隆安县| 贺州市| 古丈县| 峨眉山市| 德钦县| 西畴县| 广汉市|