小编Shr*_*i B的帖子

Angular 如何处理多个服务的依赖注入?

我创建了一个抽象类,充当其他两个服务的基础服务。以下是该抽象类的代码片段:

import { Injectable } from '@angular/core';

export interface Book {
  title: string;
  description: string;
  price: number;
} 
@Injectable({
  providedIn: 'root'
})
export abstract class BaseService {

      constructor() { }

      abstract getBooks(): Book[];

}
Run Code Online (Sandbox Code Playgroud)

然后,以下是实现抽象类服务的两个服务: 第一个服务:

export class NovelService implements BaseService {

      constructor() { }
      getBooks() {
            const novels: Book[] = [
                {
                  title: 'War and peace',
                  description: "War and Peace broadly focuses on Napoleon's invasion of Russia in 1812",
                  price: 550
                }
              ];

            return novels;
        }

}
Run Code Online (Sandbox Code Playgroud)

第二项服务:

export …
Run Code Online (Sandbox Code Playgroud)

service dependency-injection autowired angular

5
推荐指数
1
解决办法
261
查看次数