2016-04-28 132 views
0

我回来了另一个问题.. 我在本地服务器上有一个API,我想与它通信。 但是,当我做404获取angular2/http - 请求到api

import {Http, Response} from 'angular2/http'; or 
import { HTTP_PROVIDERS } from 'angular2/http'; or 
import {HTTP_BINDINGS} from 'angular2/http'; or 
import {Http, Response} from 'angular2/http'; 

我得到一个错误? “404 GET/angular2/http”。

这里是我的toolservice.ts:

import {Injectable} from 'angular2/core'; 
import {Http, Response} from 'angular2/http'; 
import {Tool} from './tool'; 
import {BackendService} from './backend.service'; 

@Injectable() 
export class ToolService { 
constructor( 
    private _backend: BackendService /*,http: Http*/){ } 

    private _tools:Tool[] = []; 

    getTools() { 
    this._backend.getAll(Tool).then((tools:Tool[]) => { 
     this._tools.push(...tools); // fill cache 
    }); 
    return this._tools; } 
    /*launchtool(){ 
    alert("I asked to launch your tool..."); 
    http.get("http://localhost:8080/"); 
    //this.http.get("http://localhost:8080/"); 
    //$http.get("http://localhost:8080/"); 
    }*/ 
} 

如果我取消“私人_backend:BackendService/,私人HTTP是:http /){}” 我得到一个错误原因的角度无法找到HTTP。

我错过了什么吗?我试着做npm install -g http和http-request但是效果不好..

+0

有人帮助我。我忘了导入我的主要html文件angular2/http。 http://stackoverflow.com/questions/34748257/angular-2-gives-systemjs-cannot-find-angular2-http-module – Ryusekai

回答

0

这是一个服务。

import { Injectable } from 'angular2/core'; 
import { Http, Response } from 'angular2/http'; 
import 'rxjs/Rx'; 
import { Observable } from 'rxjs/Observable'; 

@Injectable() 
export class BackendService { 
    constructor(private _http: Http) {} 
getAll():Observable<any> { 
return this._http.get('http://localhost:8080/') 
.map(res => res.json()); 
}; 

下面是部分

import { BackendService } from './backend.service'; 
constructor(private backendservice: BackendService) { } 
data: any[]; 

get(): void { 
this.backendservice.getAll().subscribe(data => this.data = data, 
error => console.log(error)); 
}