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

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.

主站蜘蛛池模板: 东丰县| 屏东市| 商丘市| 龙陵县| 正安县| 宾阳县| 友谊县| 独山县| 定远县| 开远市| 中方县| 黔南| 邵东县| 鹤壁市| 达日县| 武强县| 鹤峰县| 岳阳市| 邻水| 交口县| 尉氏县| 武鸣县| 赫章县| 红河县| 揭西县| 延安市| 大兴区| 博野县| 临西县| 白城市| 海宁市| 东至县| 平武县| 若尔盖县| 彰化县| 五台县| 福州市| 长海县| 漠河县| 永仁县| 广东省|