首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >尝试将http响应分配给数据源时出现角度表错误

尝试将http响应分配给数据源时出现角度表错误
EN

Stack Overflow用户
提问于 2018-08-08 01:38:07
回答 1查看 314关注 0票数 1

基于此示例https://stackblitz.com/angular/lmgdevradbre?file=app%2Ftable-http-example.ts

我正在尝试使代码适应我的api的响应。但我得到了

Type 'any[] | ConLiq' is not assignable to type '{}[]'.
  Type 'ConLiq' is not assignable to type '{}[]'.
    Property 'includes' is missing in type 'ConLiq'.

line: ).subscribe(data => this.dataSource.data = data);

为什么会这样?‘include’属性是什么?我在数据源对象中看不到它。

这个错误特别出现在this.dataSource.data

JSON

[
    {
      "con": "Sonsectetur sunt",
      "con_id": 360,
    },
    {
      "con": "Oulla dolore",
      "con_id": 933,
    }
]

TS

export class LiqConComponent implements OnInit {
    displayedColumns = ['con', 'con_id'];
    exampleDatabase: ExampleHttpDao | null;
    dataSource = new MatTableDataSource();
    isLoadingResults = true;

    @ViewChild(MatPaginator) paginator: MatPaginator;
    @ViewChild(MatSort) sort: MatSort;

    constructor(private http: HttpClient) { }

    ngOnInit() {
        this.exampleDatabase = new ExampleHttpDao(this.http);

        // If the user changes the sort order, reset back to the first page.
        this.sort.sortChange.subscribe(() => this.paginator.pageIndex = 0);

        merge(this.sort.sortChange, this.paginator.page)
            .pipe(
                startWith([]),
                switchMap(() => {
                    this.isLoadingResults = true;
                    return this.exampleDatabase!.getConLiq();
                }),
                map(data => {
                    // Flip flag to show that loading has finished.
                    this.isLoadingResults = false;

                    return data;
                }),
                catchError(() => {
                    this.isLoadingResults = false;
                    return observableOf([]);
                })
            ).subscribe(data => this.dataSource.data = data); // Here I get the error
    }
}

export interface ConLiq {
    con: string;
    con_id: number;
}

export class ExampleHttpDao {
    constructor(private http: HttpClient) { }

    getConLiq(): Observable<ConLiq> {
        const json_con = api + 'conliq';

        return this.http.get<ConLiq>(json_con);
    }
}

getConLiq()返回:

{
  "_isScalar": false,
  "source": {
    "_isScalar": false,
    "source": {
      "_isScalar": false,
      "source": {
        "_isScalar": true,
        "value": {
          "url": "api address",
          "body": null,
          "reportProgress": false,
          "withCredentials": false,
          "responseType": "json",
          "method": "GET",
          "headers": {
            "normalizedNames": {},
            "lazyUpdate": null,
            "headers": {}
          },
          "params": {
            "updates": null,
            "cloneFrom": null,
            "encoder": {},
            "map": null
          },
          "urlWithParams": "api address"
        }
      },
      "operator": {
        "concurrent": 1
      }
    },
    "operator": {}
  },
  "operator": {}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-08 06:33:21

您的代码中存在多个问题:

  • getConLiq()函数返回Observable<ConLiq>,但它应为Observable<ConLiq[]>return this.http.get<ConLiq>(json_con);也是如此,它应该是参数化类型,因此return this.http.get<ConLiq[]>(json_con); dataSource = new MatTableDataSource();行应该是dataSource = new MatTableDataSource<ConLiq>();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51732494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档