我已经浏览了这篇文章(以及这篇文章),以找到解决我问题的解决方案。
我想忽略这些错误:
Property 'payload' does not exist on type 'Matatu'.
Property 'key' does not exist on type 'Matatu'.
我已经尝试将变量声明"覆盖"类型系统,但它没有起作用。
这是我的.ts
代码,到目前为止我尝试的内容:
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ActionSheetController } from 'ionic-angular';
import { AngularFireList, AngularFireDatabase } from 'angularfire2/database';
import { Matatu } from '../../models/matatu/matatu.interface';
/* imports */
declare var payload;
declare var key;
@IonicPage()
@Component({
selector: 'page-owner-dash',
templateUrl: 'owner-dash.html',
})
export class OwnerDashPage {
matatuListRef$: AngularFireList<Matatu>;
matatuListAsync$: any;
constructor(public navCtrl: NavController, public navParams: NavParams, private database: AngularFireDatabase, public actionShtCrtl: ActionSheetController) {
this.matatuListRef$ = this.database.list('matatu-list');
this.matatuListAsync$ = this.matatuListRef$.snapshotChanges();
}
ionViewDidLoad() {
//stuff
}
selectMatatu(matatu: Matatu){
this.actionShtCrtl.create({
title: `${matatu.payload.val().matNumberPlate}`,
buttons: [
{
text: 'Edit',
handler: () => {
//stuff
}
},
{
text: 'Delete',
role: 'destructive',
handler: () => {
this.matatuListRef$.remove(matatu.key);
}
},
{
text: 'Cancel',
role: 'cancel',
handler: () => {
//do something
}
}
]
}).present();
}
}
这是Matatu
结构:
export interface Matatu {
$key?: string;
matNumberPlate: string;
matSacco: string;
matDriver: string;
matAccessCode: string;
matStatus: string;
matTracker: string;
matLocation: string;
}
我如何将属性投入任何类型?
我如何将属性投入任何类型?
如果您只想施放它以避免打字稿错误,则可以做这样的事情:
// ...
title: `${(<any>matatu).payload.val().matNumberPlate}`,
// ...
和
// ...
this.matatuListRef$.remove((<any>matatu).key);
// ...
这样,matatu
将被施加到any
,并且错误应该消失。
如果您可以修改Matatu
接口,我建议您在此添加这两个属性:
export interface Matatu {
$key?: string;
matNumberPlate: string;
matSacco: string;
matDriver: string;
matAccessCode: string;
matStatus: string;
matTracker: string;
matLocation: string;
// New properties are optional
payload?: any;
key?: any;
}
这样,您就不需要施放matatu
属性,因为payload
和key
都是Matatu
类型的定义的一部分(并且均为any
类型)。