我正在使用nativescript创建聊天应用程序,可以在其中使用特定方法加载更多消息。这些新加载的消息将添加到messages-array的开头。现在,一旦message数组发生变化,listview从中获取其项目,就会给我错误:
ERROR TypeError:无法读取未定义的属性'length'
ERROR CONTEXT {
JS: "view": {
JS: "def": {
JS: "nodeFlags": 38518785,
JS: "rootNodeFlags": 1,
JS: "nodeMatchedQueries": 0,
JS: "flags": 0,
JS: "nodes": [
JS: {
JS: "nodeIndex": 0,
JS: "parent": null,
JS: "renderParent": null,
JS: "bindingIndex": 0,
JS: "outputIndex": 0,
JS: "checkIndex": 0,
JS: "flags": 1,
JS: "childFlags": 38518785,
JS: "directChildFlags": 33554433,
JS: "childMatchedQueries": 0,
JS: "matchedQueries": {},
JS: "matchedQueryIds": 0,
JS: "references": {},
JS: "ngContentIndex": null,
JS: "childCount": 2,
JS: "bindings": [],
JS: "bindingFlags": 0,
JS: "outputs": [],
JS: "element": {
JS: "ns": "",
JS: "name": "StackLayout",
JS: "attrs": [],
JS: "template": null,
JS: "componentProvider": null,
JS: "componentView": null,
JS: "componentRendererType": null,
JS: "publicProviders": {},
JS: "allProviders": "[Ci...
这是我的方法
this.moreMessagesSub = this.socket.more_messages_receiver$.subscribe(
(messages: Message[]) => {
this.currentRoom.messages = messages.concat(this.currentRoom.messages); // This leads to the error.
this.listViewComponent.nativeElement.notifyPullToRefreshFinished();
this.changeRef.detectChanges();
}
);
在这里,我向您展示我的列表视图。
<StackLayout row="0" col="0" colSpan="2">
<RadListView #chatListView pullToRefresh="true" (pullToRefreshInitiated)="onPullToLoadMore()" [items]="currentRoom.messages" separatorColor="white" height="100%">
<ng-template tkListItemTemplate let-message="item" let-i="index">
<StackLayout>
<ns-chat-message [message]="message" ></ns-chat-message>
</StackLayout>
</ng-template>
</RadListView>
</StackLayout>
当我删除this.currentRoom.messages = messages.concat(this.currentRoom.messages);
行时,没有任何错误。
我也在这里尝试使用array.unshift,但结果完全一样。那么为什么nativescript listview在更新时会丢失数组?显然,这只是非常短的时间。错误和一些滞后之后,它继续按预期工作,甚至成功地在op中添加了消息