如何解决对象不可分配键入any []错误? [英] How to resolve object is not assignable to type any[] error?

查看:43
本文介绍了如何解决对象不可分配键入any []错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Ionic/Angular项目中,如何解决显示

In an Ionic/Angular project how would I resolve the error that says

对象"不可分配为任何[]"类型

'Object' is not assignable to type 'any[]'

到目前为止,我的代码:

my code so far:

    export class HomePage {

  todayEvents: [];

  constructor( public dataService: DataServiceProvider) {
     this.getTodayEvents();     
  }

    getTodayEvents() {
    this.dataService.getTodayEvents().subscribe(events => {
      this.todayEvents = events;     
      console.log(events);
    });

  }


}

这是在我的api调用上记录到控制台的内容:看起来像一个数组,但是我丢失了一些东西,因为打字稿不喜欢它...

And this is what is logged to console on my api call: It seems like an array but I'm missing something because typescript doesnt like it...

{data: Array(2)}
data
:
Array(2)
0
:
{id: 1, client_id: 1, starts_at: "2017-11-18 12:30:00", ends_at: "2017-11-18 14:30:00", completed: "3", …}
1
:
{id: 1, client_id: 1, starts_at: "2017-11-19 12:30:00", ends_at: "2017-06-27 14:30:00", completed: "3", …}

DataService//由于我不再需要新的common/Http,因此我删除了.map.

DataService //I remove .map because in the new common/Http is no longer required.

getTodayAppointments() {
  return this.http.get(this.apiUrl);
    // No longer necessary in http/common ....map((res: Response) => res.json());  
}

推荐答案

尝试如下:

export class HomePage {

    todayEvents: Array<any> = [];

    constructor(public dataService: DataServiceProvider) {
        this.getTodayEvents();
    }

    getTodayEvents() {
        this.dataService.getTodayEvents().subscribe(events => {
            this.todayEvents = events.data;
            console.log(events);
        });

    }
}

这篇关于如何解决对象不可分配键入any []错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆