2015-12-30 20 views
2

Angular2,現在處於測試階段,我的公司決定稍微處理它。 我試圖從我的服務設置請求。我瀏覽所有的互聯網,但沒有任何工作。 (也許帖子是在Beta版之前寫的)。@Injectable Service中的HTTP提供者

所以,我有我的boot.ts這樣的:

import {bootstrap} from 'angular2/platform/browser'; 
 
import {Component, provide} from 'angular2/core'; 
 
import {HTTP_PROVIDERS} from 'angular2/http'; 
 

 
import {BrandsComponent} from './brands/brands.component'; 
 
import {BrandsService} from './brands/brands.service'; 
 

 
@Component({ 
 
\t selector: 'my-app', 
 
\t template: ` 
 
\t \t <brands></brands> 
 
\t `, 
 
\t directives: [BrandsComponent] 
 
}) 
 

 
export class AppComponent { 
 
} 
 

 

 
bootstrap(AppComponent, [HTTP_PROVIDERS, BrandsService]);

我BrandsComponent注入我BrandsService。 這裏我的服務代碼:

import {Http} from 'angular2/http'; 
 
import {Injectable, Inject} from 'angular2/core'; 
 

 
@Injectable() 
 
export class BrandsService{ 
 
\t constructor(public http: Http) { 
 
\t \t console.log('Task Service created.', http); 
 
\t \t http.get('http://google.fr'); 
 
\t } 
 

 
\t getBrands(){ 
 
\t \t //return this.http.get('./brands.json'); 
 
\t \t return []; 
 
\t } 
 
}

在我的控制檯,我有日誌「創建任務服務」,但任何Ajax請求是怎麼回事。

我不能告訴你我試過了什麼,因爲我改變了我的代碼約10億次。

感謝您的幫助!

@Edit:

這裏我BrandsComponent代碼:

import {Component} from 'angular2/core'; 
 

 
import {Brand} from './brand.interface'; 
 
import {BrandsService} from './brands.service'; 
 

 
import {ModelsComponent} from './../models/models.component'; 
 

 
@Component({ 
 
\t selector: 'brands', 
 
\t templateUrl: 'templates/brands/list.html', 
 
\t providers: [BrandsService], 
 
\t directives: [ModelsComponent] 
 
}) 
 

 
export class BrandsComponent implements OnInit{ 
 
\t public brands; 
 
\t public selectedBrand : Brand; 
 

 
\t constructor(private _brandsService: BrandsService) { } 
 

 
\t /* 
 
\t * Get all brands from brands service 
 
\t */ 
 
\t getBrands(){ 
 
\t \t this.brands = this._brandsService.getBrands(); 
 
\t } 
 

 
\t /* 
 
\t * On component init, get all brands from service 
 
\t */ 
 
\t ngOnInit(){ 
 
\t \t this.getBrands(); 
 
\t } 
 

 
\t /* 
 
\t * Called when li of brand list was clicked 
 
\t */ 
 
\t onSelect(brand : Brand){ 
 
\t \t this.selectedBrand = brand; 
 
\t } 
 
}

+0

你能張貼BrandsComponent代碼? – Romain

+0

錯誤是什麼。因爲如果你得到了「任務服務創建」日誌,那麼首先你必須將http.get的值保存在某個地方以供使用,如果我沒有錯的話。 – binariedMe

+0

羅曼:我有我的BrandsComponent,謝謝。 @binariedMe:當然,但任何請求在開發者控制檯中都可見。我將結果賦值給var,並將控制檯記錄下來,但我有Observer對象響應。我搜尋原因,並說明。 – PallMallShow

回答

1

事實上觀測是懶惰。這意味着在使用subscribe方法附加某些響應偵聽器之前,不會發送相應的HTTP請求。

添加在你BrandsService的構造subscribe方法觸發您的HTTP請求:

import {Http} from 'angular2/http'; 
import {Injectable, Inject} from 'angular2/core'; 

@Injectable() 
export class BrandsService{ 
    constructor(public http: Http) { 
    console.log('Task Service created.', http); 
    http.get('http://google.fr').subscribe(); 
    } 
    (...) 
} 

希望它可以幫助你, 蒂埃裏

+0

謝謝蒂埃裏。實際上,使用訂閱方法,請求已正確啓動。在研究了subscribe方法之後,我們可以在參數中設置來自響應的數據,如:'.subscribe(data => this.brands = data.json());'。 如果我console.log(data.json()),這很好,我有一個對象。但是,如果我返回this.brands,它將返回undefined。也許返回是在ajax響應之前設置的? – PallMallShow

+0

是的,事實上,由於您處於箭頭函數(即回調)中,因此無法返回'this.brands'。您應該返回observable:'http http.get('http://google.fr');'並在調用服務的組件內調用'subscribe'方法。這個答案應該可以幫助你:http://stackoverflow.com/questions/34450131/how-to-consume-http-component-efficiently-in-angular-2-beta/34450948#34450948 ;-) –

+0

太棒了。感謝您的提議!我從我的服務返回http方法,我在服務被調用的ma組件中,我在這裏設置了訂閱和所有作品。我希望這篇文章可以幫助很多人;) – PallMallShow

相關問題