2016-03-01 43 views

回答

1

你可以使用類似的東西:

import {Http, URLSearchParams} from 'angular2/http'; 

@Injectable() 
export class SomeHttpService { 
    constructor(http) { 
    this.http = http; 
    } 

    getSomething() { 
    URLSearchParams params = new URLSearchParams(); 
    params.set('id', '1'); 
    return this.http.get('http://...', { search: params }).map(res => res.map()); 

    /* 
     or if you need to subscribe in the service 

     this.http.get('http://...').map(res => res.map()) 
       .subscribe(
       (data) => { 
        // do something with data 
       } 
       ); 
    */ 
    } 

    static get parameters() { 
    return [[Http]]; 
    } 
} 

不要忘了導入Angular2的HTTP模塊文件:

<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script> 
<script src="node_modules/systemjs/dist/system.src.js"></script> 
<script src="node_modules/rxjs/bundles/Rx.js"></script> 
<script src="node_modules/angular2/bundles/angular2.dev.js"></script> 
<script src="node_modules/angular2/bundles/http.dev.js"></script> <--- 

引導您的應用程序時設置HTTP_PROVIDERS提供商:

import {HTTP_PROVIDERS} from 'angular2/http'; 
(...) 

bootstrap(AppComponent, [ HTTP_PROVIDERS ]); 

實際上,唯一特定於ES6的是w唉配置依賴注入與靜態獲取...

+0

我如何通過參數,例如'id = 1' –

+0

使用'URLSearchParams',但它不是特定於ES6的東西...我更新了我的答案 –

+0

謝謝對於我們的幫助,我將tr併發布結果 –