出现以下错误: 类型'Promise'不能分配给'string []'."承诺"类型中缺少属性"包含".
当我施放Promise键入'string []'我的代码如下,
组件:app.dashboard.ts
import {Component} from '@angular/core';
import { MemberService } from "./app.service";
@Component({
selector:'app-root',
templateUrl:'./app.dashboard.html',
providers:[MemberService]
})
export class AppDashboard{
title='Dashboard'
constructor(private memberService: MemberService) { }
public doughnutChartLabels:string[] =
this.memberService.getmemberheader();//error occurred here
}
}
Run Code Online (Sandbox Code Playgroud)
服务:app.service.ts
import { Injectable } from '@angular/core';
import { Member } from './Member';
import { Http, Response, Headers, RequestOptions, URLSearchParams } from'@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class MemberService
{
constructor(private http: Http) {
}
private getHeaders(){
// I included these headers because otherwise FireFox
// will request text/html instead of application/json
let headers = new Headers();
headers.append('Accept', 'application/json');
return headers;
}
getmemberheader(): Promise<string[]> {
return this.http
.get(`/ReportService/MemberDatabaseCountryname`, {headers: this.getHeaders()})
.toPromise()
.then(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body || {};
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}
Run Code Online (Sandbox Code Playgroud)
Pen*_*gyy 14
假设你的响应http.get是一个数组,在这里你Promise从函数返回memberService.getmemberheader,你应该在它的then回调中检索promise的结果(不将promise本身分配给数组doughnutChartLabels).
public doughnutChartLabels: string[];
this.memberService.getmemberheader().then(res => {
this.doughnutChartLabels = res;
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26207 次 |
| 最近记录: |