2017-07-27 69 views
0
import { Injectable } from '@angular/core'; 
import { Http, Headers, Response, RequestOptions, } from '@angular/http'; 
@Injectable() 
export class RoleService { 
    headers = new Headers({"Content-Type": "application/json"}); 
    options = new RequestOptions({ headers: this.headers }); 
    constructor(private http: Http) { } 

    getRoleList(data) { 
     return this.http.post('http://192.168.10.178:9080/role/getRole', data, this.options) 
       .toPromise() 
       .then(res => res.json().data) 
       .then(data => { return data; }); 
    } 
} 

https://i.stack.imgur.com/nPiK8.pngangular4 http头文件,如何设置?

帮助我!如何解决这个问题?

+0

请包括一个清晰的问题,有什么问题,结果是什么,等等... – Vega

回答

2

尝试

export class RoleService { 
    options: RequestOptions; 

    constructor(private http: Http) { 
    let headers: any = new Headers(); 
    headers.append('Content-Type', 'application/json'); 

    this.options = new RequestOptions({ headers: headers }); 
    } 

// ........ 
+0

错误:属性“追加'在'Headers'类型上不存在。 –

+0

@yanWu试着改变'headers:Headers;'到'headers:any;' – samAlvin

+0

wa ~~~你真棒!谢谢 –

0

它可能会解决您的问题

import { Injectable } from '@angular/core'; 
import { Http, Headers, Response, RequestOptions, } from '@angular/http'; 
@Injectable() 
export class RoleService { 
    constructor(private http: Http) { } 

    getRoleList(data) { 
    let headers = new Headers({"Content-Type": "application/json"}); 
    let options = new RequestOptions({ headers: headers }); 
     return this.http.post('http://192.168.10.178:9080/role/getRole', data, this.options) 
       .toPromise() 
       .then(res => res.json().data) 
       .then(data => { return data; }); 
}} 
+0

同样的错误..... –