原文: https://howtodoinjava.com/angular/angular-component/

在本 Angular2 组件教程中,学习 Angular 中的组件是什么,如何创建 Angular 组件,Angular 组件元数据的示例。

1. 什么是 Angular 组件

Angular 组件控制屏幕的称为视图的部分。 支持视图中各种功能(例如数据绑定事件绑定等)的应用逻辑被编写在一个类文件中,该文件通常被称为“app.component.ts”。

2. 何时使用 Angular 组件

当应用基于基于组件的架构且用户界面分为较小的 Web 组件(每个组件提供不同的功能)时,应使用 Angular 组件。

例如,一个网站可能有一个组件用于捕获反馈,而另一个则用于社交媒体跟踪。

3. 如何创建 Angular 组件

我们可以使用 angular CLI(命令行界面)或手动创建 Angular 组件。

3.1 使用@angular/cli创建组件

可以使用以下命令,使用@angular/cli创建新的 Angular 组件(例如“邮政编码”)组件:

  1. // command: ng generate component [name]
  2. $ ng generate component zipcode

上面的命令将在src下名为zipcode的新文件夹中创建以下文件:

  • zipcode.component.html:此 html 文件包含与组件关联的代码/模板,例如:
    1. <div><strong>zipcode works!</strong></div>
  • zipcode.component.spec.ts :此文件包含与单元测试相关的代码,例如:

    1. import { async, ComponentFixture, TestBed } from '@angular/core/testing';
    2. import { ZipcodeComponent } from './zipcode.component';
    3. describe('ZipcodeComponent', () => {
    4. let component: ZipcodeComponent;
    5. let fixture: ComponentFixture<ZipcodeComponent>;
    6. beforeEach(async(() => {
    7. TestBed.configureTestingModule({
    8. declarations: [ ZipcodeComponent ]
    9. })
    10. .compileComponents();
    11. }));
    12. beforeEach(() => {
    13. fixture = TestBed.createComponent(ZipcodeComponent);
    14. component = fixture.componentInstance;
    15. fixture.detectChanges();
    16. });
    17. it('should create', () => {
    18. expect(component).toBeTruthy();
    19. });
    20. });
  • zipcode.component.ts:包含支持视图功能的逻辑的组件类。

    1. import { Component, OnInit } from '@angular/core';
    2. @Component({
    3. selector: 'app-zipcode',
    4. templateUrl: './zipcode.component.html',
    5. styleUrls: ['./zipcode.component.css']
    6. })
    7. export class ZipcodeComponent implements OnInit {
    8. constructor() { }
    9. ngOnInit() {
    10. }
    11. }
  • zipcode.component.css:CSS 文件包含与组件关联的样式表,例如:
    1. /* ZipcodeComponent's private CSS styles */
    2. h1 {
    3. font-size: 1.2em;
    4. color: #999;
    5. margin-bottom: 0;
    6. }
    7. h2 {
    8. font-size: 2em;
    9. margin-top: 0;
    10. padding-top: 0;
    11. }

3.2 手动创建组件

我们可以根据需要手动创建上述文件,但是要创建组件,我们只需要在文件夹zipcode内定义必需文件zipcode.component.ts

4. 如何导入 Angular 组件

创建完组件后,我们需要通过将其导入文件“app.module.ts”中来告知 angular 来加载组件,如下所示:

  1. import { NgModule } from '@angular/core';
  2. import { BrowserModule } from '@angular/platform-browser';
  3. import { FormsModule } from '@angular/forms';
  4. import { HttpClientModule } from '@angular/common/http';
  5. import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';
  6. import { InMemoryDataService } from './in-memory-data.service';
  7. import { AppRoutingModule } from './app-routing.module';
  8. import { AppComponent } from './app.component';
  9. import { DashboardComponent } from './dashboard/dashboard.component';
  10. import { HeroDetailComponent } from './hero-detail/hero-detail.component';
  11. import { HeroesComponent } from './heroes/heroes.component';
  12. import { HeroSearchComponent } from './hero-search/hero-search.component';
  13. import { MessagesComponent } from './messages/messages.component';
  14. import { ZipcodeComponent } from './zipcode/zipcode.component';
  15. @NgModule({
  16. imports: [
  17. BrowserModule,
  18. FormsModule,
  19. AppRoutingModule,
  20. HttpClientModule,
  21. // The HttpClientInMemoryWebApiModule module intercepts HTTP requests
  22. // and returns simulated server responses.
  23. // Remove it when a real server is ready to receive requests.
  24. HttpClientInMemoryWebApiModule.forRoot(
  25. InMemoryDataService, { dataEncapsulation: false }
  26. )
  27. ],
  28. declarations: [
  29. AppComponent,
  30. DashboardComponent,
  31. HeroesComponent,
  32. HeroDetailComponent,
  33. MessagesComponent,
  34. HeroSearchComponent,
  35. ZipcodeComponent
  36. ],
  37. bootstrap: [AppComponent]
  38. })
  39. export class AppModule { }

如果使用@angular/cli命令ng generate component zipcode来生成组件,则它将自动进行上述突出显示的更改,否则我们必须手动进行。

5. Angular 组件元数据

组件元数据是指在@Component装饰器中定义的属性@Component装饰器将紧随其后的类标识为组件类。

元数据直接通过内联代码或通过引用将模板与组件关联。 组件及其模板一起描述了视图

  1. import { Component, OnInit } from '@angular/core';
  2. @Component({
  3. selector: 'app-zipcode',
  4. templateUrl: './zipcode.component.html',
  5. styleUrls: ['./zipcode.component.css']
  6. })
  7. export class ZipcodeComponent implements OnInit {
  8. constructor() { }
  9. ngOnInit() {
  10. }
  11. }

以下是元数据中定义的最常用的属性:

  • selector:CSS 选择器可帮助 Angular 在模板 HTML 中找到的相应标记的位置创建并插入组件的实例。
  • templateUrl :组件的 HTML 模板的模块相对地址。
  • template:在//html stuff goes here中定义的内联 HTML 模板。
  • styleUrls :一个或多个 URL,用于包含 CSS 样式表的文件,仅特定于此组件。
  • style:特定于此组件的一个或多个内联 CSS 样式表

学习愉快!

阅读更多: Angular Docs