2016-07-26 86 views
2

是否有可能手动实例化Http服务而无需将其作为构造函数参数进行放置?Angular 2 - 实例化(Http)服务手动

export class SimpleGridServer { 
    private http: Http; 
    constructor(public options: SimpleServerData) { 
     http = new Http(.../* Argument here */); 
    } 
} 

要实例化这个类。

var grid = new SimpleGridServer({/* options */}); 

我想实例化这个类,而不必为我component那些进口SimpleGridServer上HTTP服务的依赖。 如果可能,这种情况有什么缺点?

回答

3

如果这是可能的,这种情况有什么缺点?

您需要在Angular DI工作流程中使用Angular的东西。

你可以得到一个处理直接角的喷油器:

import {ReflectiveInjector} from '@angular/core'; 
import {HTTP_PROVIDERS, Http, Headers} from "@angular/http"; 
const injector = ReflectiveInjector.resolveAndCreate([HTTP_PROVIDERS]); 
const http = injector.get(Http); 
+0

我想这是怎么角2点的工作方式。 – janmvtrinidad

+1

您可以使用角度2 DI而无需构造函数检查http://stackoverflow.com/a/37812906/1401751 –