类型'Promise <string []>'不能分配给类型'string []'

阿伦·库玛

出现以下错误:类型'Promise'不能分配给类型'string []'。类型“ Promise”中缺少属性“ includes”。

当我将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
  }
}

服务: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);
  }
}
蓬伊

假设您的响应来自http.get一个数组,这里您Promise要从function返回memberService.getmemberheader,您应该then改为在其回调中获取promise的结果(而不是将promise本身分配给array doughnutChartLabels)。

public doughnutChartLabels: string[];

this.memberService.getmemberheader().then(res => {
  this.doughnutChartLabels = res;
})

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

类型'promise.Promise <string>'不能分配给类型'Promise <string>'

'Promise <any>'类型的参数不能分配给'string'类型的参数

参数类型 'String"?"' 不能分配给参数类型 'String'

类型'string'不能分配给类型'{value:string; }'

在编辑器中不能分配给类型'Expected <Promise <string >>'的参数

类型 'string' 不能分配给类型 '"foo"'

参数类型“RxString”不能分配给参数类型“String”

“Params”类型的参数不能分配给“string”类型的参数

'number'类型的参数不能分配给'string'类型的参数

类型 'any[]' 不能分配给类型 '[id: string]'

“any[]”类型的参数不能分配给“string”类型的参数

TypeScript 错误:类型 'string' 不能分配给类型 'number | ""'

输入'string | 数字”不能分配给“从不”类型

类型 'Promise<void>' 不可分配给锚标记中的类型 'string'

'string | 类型的参数 null' 不能分配给类型为 'String' 的参数。类型 'null' 不能分配给类型 'String'。[角度]

不能将“Set<String?>”类型的值分配给“Map<String, String>”类型的变量

Angular Ivy类型检查:类型'SafeHtml'不能分配给类型'string'

'string | 类型的参数 number' 不能分配给类型为 'string' 的参数。“数字”类型不能分配给“字符串”类型

参数类型“List<DropdownMenuItem<String>>”不能分配给参数类型“List<String>”

即使在 .filter 函数删除 undefined 之后,Typescript 类型 (string | undefined ) 也不能分配给类型 string[]

Angular TypeScript:“类型 '(string | undefined)[]' 不能分配给类型 'string[]'。”

输入'字符串| string[]' 不能分配给类型 'string' - 打字稿类型错误

'(string | undefined)[]' 类型的参数不能分配给 'string[]' 类型的参数

TypeScript-类型'String'的参数不能分配给类型'string'的参数

参数类型 'Map<String, Object>' 不能分配给参数类型 'String'.dartargument_type_not_assignable

'string | 类型的参数 null' 不能分配给类型为 'string' 的参数

参数类型 'Map<String, dynamic> Function()' 不能分配给参数类型 'Map<String, dynamic>'

颤振:“Future<String>”类型的值不能分配给“String”类型的变量

参数类型 'string | null' 不能分配给参数类型 'string | 数量 | 布尔值'