Angular2 access global service without including it in every constructor

后端 未结 2 660
Happy的楠姐
Happy的楠姐 2021-01-06 22:38

I have three classes:

@Injectable()
export class ApiService {
  constructor(public http: Http) {}

  get(url: string) {
    return http.get(url);
  }

}

@In         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-06 23:00

    I think that it's not something supported. It's the way dependency injection works in Angular2, i.e. based on decorators. The latters uses the constructor of the associated class to determine what must be injected. You must be aware that the concept of class and extends are natively supported by TypeScript but such code is transpiled into JavaScript.

    See this answer for more details about the transpiled code:

    • How to inject service to class and then extend component with it?

    Hope it helps you, Thierry

提交回复
热议问题