首页 > 解决方案 > “别名”类型的参数不可分配给“任何 []”类型的参数

问题描述

当我现在更新角度版本时,我遇到了主题中的错误。我应该如何解决这个问题?

我的界面如下:

export interface Alias {
    nr?: number;
    local_part?: string;
    domain?: string;
    recipients?: string;
    username?: string;
    changes?: string;
    action?: string;
    old_alias?: string;
}

在我的 component.ts

export class HomeComponent implements OnInit {
  alias: Alias

我想从后端获取数据

  this.aliasesService.getAlias(params).subscribe(
    res => {
      var result = Object.keys(res).map(function (key) {
        return [Number(key), res[key]];
      });
      this.alias = res;
      },
    err => console.error(err)
  );

有错误信息

ERROR in src/app/home/home.component.html:154:44 - error TS2345: Argument of type 'Alias' is not assignable to parameter of type 'any[]'.

Type 'Alias' is missing the following properties from type 'any[]': length, pop, push, concat, and 26 more.
    
154 *ngFor="let aliases of alias | filterBy: queryString | paginate: { itemsPerPage: 10, currentPage: p }; let i = index">

标签: angulartypescript

解决方案


这可能会发生,因为在您的服务中,您将返回设置为Observable<any[]>,而不是数组,删除括号,然后它将是这样的: Observable<any>


推荐阅读