2016-04-28 77 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)); 
}