是否可以手动实例化 Http Service,而不将其作为构造函数参数?
export class SimpleGridServer {
private http: Http;
constructor(public options: SimpleServerData) {
http = new Http(.../* Argument here */);
}
}
实例化此类。
var grid = new SimpleGridServer({/* options */});
我想实例化此类,但不依赖于导入 SimpleGridServer
的 组件
的 Http 服务。
如果这是可能的,这种情况的缺点是什么?
最佳答案
If this is possible, what is the drawback for this scenario?
您需要处于 Angular DI 工作流程中才能使用 Angular 的东西。
您可以直接获取 Angular 注入(inject)器的句柄:
import {ReflectiveInjector} from '@angular/core';
import {HTTP_PROVIDERS, Http, Headers} from "@angular/http";
const injector = ReflectiveInjector.resolveAndCreate([HTTP_PROVIDERS]);
const http = injector.get(Http);
关于typescript - Angular 2 - 手动实例化 (Http) 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38579941/