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

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.

主站蜘蛛池模板: 扶绥县| 万山特区| 淄博市| 甘孜| 云和县| 廉江市| 珠海市| 昌邑市| 张家港市| 江都市| 卓资县| 凌海市| 万全县| 图木舒克市| 青河县| 夏河县| 双峰县| 泗洪县| 岗巴县| 荣成市| 阜平县| 海南省| 灵川县| 黄山市| 宜良县| 丰县| 平南县| 革吉县| 阿拉善右旗| 米林县| 乌审旗| 鄄城县| 垫江县| 苏尼特左旗| 大化| 大冶市| 马边| 宁河县| 平远县| 延边| 建宁县|