我正在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;
});
}
此按钮上的点击事件调用此功能,其中包含&#34;选项 - 删除&#34;
<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 => {
似乎有问题。
答案 0 :(得分:2)
Array.prototype.map
将使用object参数而不是array的函数。在这种情况下不需要使用它。
尝试:
removeChat(chat: Chat): void {
const chatIndex = this.chats.indexOf(chat);
this.chats.splice(chatIndex,1);
}