我正在 https://angular-meteor.com/tutorials/whatsapp2/ionic/chats-page 上为Ionic 2应用程序学习教程。添加以下代码时收到错误:
removeChat(chat: Chat): void {
this.chats = this.chats.map<Chat[]>(chatsArray => {
const chatIndex = chatsArray.indexOf(chat);
chatsArray.splice(chatIndex, 1);
return chatsArray;
});
}
此函数在具有类"option-remove"的按钮上的单击事件上调用
<ion-content class="chats-page-content">
<ion-list class="chats">
<ion-item-sliding *ngFor="let chat of chats | async">
<button ion-item class="chat">
<img class="chat-picture" [src]="chat.picture">
<div class="chat-info">
<h2 class="chat-title">{{chat.title}}</h2>
<span *ngIf="chat.lastMessage" class="last-message">
<p *ngIf="chat.lastMessage.type == 'text'" class="last-message-content last-message-content-text">
{{chat.lastMessage.content}}
</p>
<span class="last-message-timestamp">{{chat.lastMessage.createdAt | amCalendar }}</span>
</span>
</div>
</button>
<ion-item-options class="chat-options">
<button ion-button color="danger" class="option option-remove" (click)="removeChat(chat)">Remove</button>
</ion-item-options>
</ion-item-sliding>
</ion-list>
不确定错误消息的准确性如何,但似乎行this.chats.map<Chat[]>(chatsArray => {
有问题。
Array.prototype.map
将采用对象参数而不是数组的函数。在这种情况下没有必要使用。尝试:
removeChat(chat: Chat): void {
const chatIndex = this.chats.indexOf(chat);
this.chats.splice(chatIndex,1);
}