[Angular] Using useExisting provider
Angular Using Provider
2023-09-14 08:59:18 时间
Unlike 'useClass', 'useExisting' doesn't create a new instance when you register your service inside ngmodule.
'useExisting' also can limit the function call, for example:
import { Injectable, Inject } from '@angular/core'; import { Http } from '@angular/http'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map'; @Injectable() export class FoodService { constructor( private http: Http ) { } getSides(): Observable<any[]> { return this.http.get('/api/sides') .map(response => response.json()); } getPizzas(): Observable<any[]> { return this.http.get('/api/pizzas') .map(response => response.json()); } getDrinks(): Observable<any[]> { return this.http.get('/api/drinks') .map(response => response.json()); } }
We have a service which has three methods 'getSides', 'getPizzas' & 'getDrinks'.
Inside one of component, we use 'useExisting' to create an abstract class 'DrinkService', which return include one methods which 'DrinkService' needs -- getDrinks()
import { Component, OnInit } from '@angular/core'; import { Http } from '@angular/http'; import { Observable } from 'rxjs/Observable'; import { FoodService } from '../food.service'; interface Drink { name: string, price: number } export abstract class DrinkService { getDrinks: () => Observable<Drink[]>; } @Component({ selector: 'drink-viewer', providers: [ { provide: DrinkService, useExisting: FoodService } ], template: ` <div> <div *ngFor="let item of items$ | async"> {{ item.name }} {{ item.price | currency:'USD':true }} </div> </div> ` }) export class DrinkViewerComponent implements OnInit { items$: Observable<Drink[]>; constructor(private foodService: DrinkService) {} ngOnInit() { this.items$ = this.foodService.getDrinks(); } }
This can help TypeScript to limit the methods available to our Drink component, because in drink component, we don't need 'getPizzas' or 'getSides'. But this is just compile checking, it will still call the methods 'getPizzas' or 'getSides' during run time.
相关文章
- [Angular] Send Data via HTTP using Angular HttpParams
- [Angular] Two ways to create Angular Animation, using animation() or using state()
- [Angular HTML] Implementing The Input Mask Cursor Navigation Functionality -- setSelectionRange
- [Angular] USING ZONES IN ANGULAR FOR BETTER PERFORMANCE
- [Angular 2] ROUTING IN ANGULAR 2 REVISITED
- [Angular 2] @ngrx/devtools demo
- [Angular 2] Using Promise to Http
- [AngualrJS] Using Angular-Cache for caching http request
- [Angular 2] Using Pipes to Filter Data
- [AngularJS] Using angular.bootstrap to Initialize Your App
- [Angular] USING ZONES IN ANGULAR FOR BETTER PERFORMANCE
- [Angular 2] Using ng-for to repeat template elements
- [Angular 2] Using ng-model for two-way binding
- [Angular + Unit] AngularJS Unit testing using Karma
- [Angular 2] Template property syntax
- [AngularJS] Using the Angular scope $destroy event and method
- Angular和SAP C4C的事件处理队列
- Angular 模块封装概念常见的错误理解
- 如何解决 Angular custom library module 在 ng build 时无法被识别的错误
- Angular应用里的@Input和@Output注解使用方法介绍