Adding Components, Pipes and Services to a Module

In the previous section, we learned how to create a module with just one component but we know that is hardly the case. Our modules are usually made up of multiple components, services, directives and pipes. In this chapter we are going to extend the example we had before with a custom component, pipe and service.

Let's start by defining a new component that we are going to use to show credit card information.

credit-card.component.ts

  1. import { Component, OnInit } from '@angular/core';
  2. import { CreditCardService } from './credit-card.service';
  3. @Component({
  4. selector: 'app-credit-card',
  5. template: `
  6. <p>Your credit card is: {{ creditCardNumber | creditCardMask }}</p>
  7. `
  8. })
  9. export class CreditCardComponent implements OnInit {
  10. creditCardNumber: string;
  11. constructor(private creditCardService: CreditCardService) {}
  12. ngOnInit() {
  13. this.creditCardNumber = this.creditCardService.getCreditCard();
  14. }
  15. }

This component is relying on the CreditCardService to get the credit card number, and on the pipe creditCardMask to mask the number except the last 4 digits that are going to be visible.

credit-card.service.ts

  1. import { Injectable } from '@angular/core';
  2. @Injectable()
  3. export class CreditCardService {
  4. getCreditCard(): string {
  5. return '2131313133123174098';
  6. }
  7. }

credit-card-mask.pipe.ts

  1. import { Pipe, PipeTransform } from '@angular/core';
  2. @Pipe({
  3. name: 'creditCardMask'
  4. })
  5. export class CreditCardMaskPipe implements PipeTransform {
  6. transform(plainCreditCard: string): string {
  7. const visibleDigits = 4;
  8. let maskedSection = plainCreditCard.slice(0, -visibleDigits);
  9. let visibleSection = plainCreditCard.slice(-visibleDigits);
  10. return maskedSection.replace(/./g, '*') + visibleSection;
  11. }
  12. }

With everything in place, we can now use the CreditCardComponent in our root component.

app.component.ts

  1. import { Component } from "@angular/core";
  2. @Component({
  3. selector: 'app-root',
  4. template: `
  5. <h1>My Angular App</h1>
  6. <app-credit-card></app-credit-card>
  7. `
  8. })
  9. export class AppComponent {}

Of course, to be able to use this new component, pipe and service, we need to update our module, otherwise Angular is not going to be able to compile our application.

app.module.ts

  1. import { NgModule } from '@angular/core';
  2. import { BrowserModule } from '@angular/platform-browser';
  3. import { AppComponent } from './app.component';
  4. import { CreditCardMaskPipe } from './credit-card-mask.pipe';
  5. import { CreditCardService } from './credit-card.service';
  6. import { CreditCardComponent } from './credit-card.component';
  7. @NgModule({
  8. imports: [BrowserModule],
  9. providers: [CreditCardService],
  10. declarations: [
  11. AppComponent,
  12. CreditCardMaskPipe,
  13. CreditCardComponent
  14. ],
  15. bootstrap: [AppComponent]
  16. })
  17. export class AppModule { }

Notice that we have added the component CreditCardComponent and the pipe CreditCardMaskPipe to the declarations property, along with the root component of the module AppComponent. In the other hand, our custom service is configured with the dependency injection system with the providers property.

View Example

Be aware that this method of defining a service in the providers property should only be used in the root module. Doing this in a feature module is going to cause unintended consequences when working with lazy loaded modules.

In the next section, we are going to see how to safely define services in feature modules.

原文: https://angular-2-training-book.rangle.io/handout/modules/multiple-elements.html