角度2:Http获取/发布请求

时间:2016-10-03 06:10:30

标签: angular

  

错误:错误:无法解析HttpTestComponent的所有参数:(?)

我想做http请求,我也创建了一个服务文件,但是得到了上面的错误..

HttpTestComponent.ts

import { Component } from '@angular/core';
import {HttpTestService} from 'app/http-test.service';

@Component ({
selector: 'http-test',
template: `
          <button (click) = "onTestGet()"> Get Request </button>
          <p>{{getData}}</p>,
           <button (click) = "onTestPost()"> Post Request </button>
          <p>{{postData}}</p>` ,
providers: [HttpTestService]

})
export class HttpTestComponent {
    getData : string;
    postData : string;
    constructor(private httpService:HttpTestService){

    onTestGet() {
      this.httpService.getCurrentTime()
            .subscribe(
              data =>this.getData = JSON.stringify(data),
              error=>alert(error),
              () => console.log("finished")
      );
    }

    onTestPost() {
      this.httpService.getCurrentTime()
            .subscribe(
              data =>this.postData = JSON.stringify(data),
              error=>alert(error),
              () => console.log("finished")
      );
    }
  }
}

HTTPTestService.ts

import { Injectable }    from '@angular/core';
import { Headers, Http } from '@angular/http';

import 'rxjs/add/operator/map';

@Injectable()

export class HTTPTestService {

constructor(private http:Http){}
getCurrentTime(){
   return this.http.get('http://date.jsontest.com')
    .map(res=>res.json());

}

postJSON() {
  var json = json.stringify({var1:'test',var2:3});
  var params = 'json=' + json;
  var headers = new Headers();
  headers.append('Content-Type','application/x-www-form-urlencoded');

    return this.http.post('http://validate.jsontest.com',params,     {headers:headers})
    .map(res=>res.json());

}

}

我还添加了服务文件

0 个答案:

没有答案