本文介绍了类型{...}的参数不能分配给类型[]的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { EventDetail } from '../../models/event-detail/event-detail.interface';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';

@IonicPage()
@Component({
  selector: 'page-event',
  templateUrl: 'event.html',
})
export class EventPage {

  eventDetail = {} as EventDetail;

  eventDetailRef$: AngularFireList<EventDetail[]>;

  constructor(public navCtrl: NavController, public navParams: NavParams, private database: AngularFireDatabase) {
  this.eventDetailRef$ = this.database.list('event-detail');
  }

  addEvent( eventDetail: EventDetail) {

    this.eventDetailRef$.push({
      eventName: this.eventDetail.eventName,
      eventDesc: this.eventDetail.eventDesc,
    });

  }

}

我在一年前遵循了一个教程,我推荐了升级到Angularfire 5.0",用谷歌搜索了同样的问题,结果却大不相同

i followed a tutorial way back a year ago, i've rad "Upgrading to Angularfire 5.0", googled the same problem, result are quite different

错误消息是:

类型'{eventName:string; eventDesc:String;}'的参数不能分配给'EventDetail []'类型的参数.对象文字只能指定已知属性,并且'EventName'在'EventDetail []'类型中不存在."

"Argument of type '{ eventName: string; eventDesc: String; }' is not assignable to parameter of type 'EventDetail[]'. Object literal may only specify known properties, and 'eventName' does not exist in type 'EventDetail[]'."

和我的EventDetail界面:

and my EventDetail Interface:

export interface EventDetail {
    eventName: string;
    eventDesc: String;
    lat: number;
    lgt: number;

}

错误图片

推荐答案

eventDetailRef $:AngularFireList< EventDetail []> ;; 更改为 eventDetailRef $:AngularFireList< EventDetail> >

因为您的类型是 Array< Array> ,并且您尝试推送单个项目

Because because your type is Array<Array> and you trying to push a single item

这篇关于类型{...}的参数不能分配给类型[]的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 14:01