2016-11-06 147 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”仅指定类型。你可以把这个类型关掉,它会是一样的(类型暗示) –