2016-11-06 127 views
0

我嘗試了一切,並且我無法獲得http請求,無法通過heroku訪問我的節點服務器。我可以手動命中路由,所以它不是服務器。我將粘貼我的服務和我的頁面。Angular 2 Http無法正常工作

**類是subscription.service.ts

import {Http, Response} from '@angular/http' 
import {Injectable} from '@angular/core' 
import 'rxjs/add/operator/map'; 

@Injectable() 
export class SubscriptionService { 

    http:Http; 
    constructor(http:Http){ 
    this.http = http; 
    } 

    getEntries() { 
    return this.http.get('my url here *****').map(res => res.json()); 
    } 
} 

**類是dashboard.component.ts

import {Component, ViewEncapsulation} from '@angular/core'; 
import {SubscriptionService} from '../../_services/subscription.service'; 
import 'rxjs/Rx'; 

@Component({ 
    selector: 'dashboard', 
    providers: [SubscriptionService], 
    encapsulation: ViewEncapsulation.None, 
    styles: [require('./dashboard.scss')], 
    template: require('./dashboard.html') 
}) 
export class Dashboard { 
    getData: string; 

    constructor(private subscriptionService: SubscriptionService) { 
    } 

    ngOnInit() { 
    console.log("test!!"); 
    this.subscriptionService.getEntries() 
     .subscribe(data => this.getData = JSON.stringify(data), 
     error => console.log("Error!"), 
     () => console.log("finished!") 
    ); 
    } 
} 

我ngOnInit()被調用,我看到控制檯打印,但在heroku上沒有任何請求顯示在日誌中。在控制檯中也沒有錯誤出現。

+0

轉到Chrome檢查器的「Sources」標籤,並打開「暫停異常」並打開「暫停未捕獲的異常」。然後重新運行你的代碼。是否有錯誤? –

+0

什麼都沒有出現。 – Hydtek

回答

1

確保您已經以root身份導入了HttpModule。 我沒有看到任何可以導致這種情況的東西。爲了確保http正在工作,您可以在SubscriptionService上的getEntries方法中放置一箇中斷點,並按照指示它的位置進行操作。

更新: - 正如@peeskillet指出的那樣,您的依賴沒有任何問題。嘗試使用更多信息調試和更新您的問題。

+1

您所展示的功能是等同的。添加訪問修飾符只是簡短的,並將其分配給類屬性。沒有訪問修飾符,沒有屬性被添加到類中,並且構造函數arg在構造函數 –

+0

中不可用,在這種情況下不應該使用這個 http:Http; 構造函數(http:http){ this.http = http; } 是 http:Http; 構造函數(http){ this.http = http; }? –

+0

不需要。「Http」(在構造函數中)是注入的標記。類「http:Http」僅指定類型。你可以把這個類型關掉,它會是一樣的(類型暗示) –