The service class for testing:
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root',})export abstract class HttpErrorHandler{
constructor(){}
abstract handleError(
): void;
}
@Injectable({
providedIn: 'root',})export class UnKnownHandler {
handleError(){
console.log('UnknownHandler works'); }}@Injectable({
providedIn: 'root',})export class BadGatewayHandler {
handleError(){
console.log('BadGatewayHandlers works'); }}Copy the code
Because abstract class can’t be instantiated, the above definition gets an error message:
Cannot assign an abstract constructor type to a non-abstract constructor type.
Solutions:
The runtime is injected with an array of HttpErrorHandler subclasses:
More of Jerry’s original articles can be found in “Wang Zixi” :