【发布时间】:2019-10-02 12:04:42
【问题描述】:
当页面加载时,数据被正确获取并显示,然后在离开页面时再次获取数据并加倍到当前数组中。如果我有项目:A,B,C,那么我将拥有A,B,CA,B,C。
现在我有一个简单的组件来检查我是否需要再次获取数据。但是,它似乎被绕过了。我看过thisand thisas well as this。
//home.ts
export class Home implements OnInit, OnDestroy{
conversations: Array<any>;
async ngOnInit() {
this._chatInit();
}
private async _chatInit() {
this.dataService.loadConversations(); //getter for local storage
const data = this.messageStorage.hasChats();
if (data.length) {
//there is data already loaded
this.conversations = data;
} else {
//there is an empty array, subscribe to it.
this.messageStorage
.getChatList$()
.subscribe(conversation => {
console.log('Conversation', conversation)
this.conversations = conversation;
});
}
}
//dataService
export class DataService {
//the object where it is all stored
private conversations: any = {
peerToPeer: {},
groups: {},
};
private listOfChats: Array<any> = new Array<any>();
private bsListOfChats$: BehaviorSubject<any> = new BehaviorSubject<any>(this.listOfChats);
public loadConversations() {
this.storage.get('conversations').then(chat=> {
this.conversations = chat;
this.formatChats(this.conversations);//converts the Object to an Array so *ngFor directive can be used
});
}
public hasChats() {
return this.listOfChats;
}
public getChatList$() {
return this.bsListOfChats$;
}
}
澄清一下,我想要加载一次n 聊天。当我离开页面并返回时,我希望加载相同的 n,不多也不少。
感谢大家的帮助!
【问题讨论】:
标签: angular typescript