繁体   English   中英

在 Ionic 应用程序中从模式导航到页面时如何更新数组?

[英]How to update array when navigating from modal to page in Ionic app?

以下是我在 Ionic 5 / Angular 应用程序中使用的ConversationMessage模型:

export class Message {
    constructor(
        public id: string,
        public text: string,
        public userId: string,
        public timestamp: string
    ) { }
}

export class Conversation {
    constructor(
        public id: string,
        public userId: string,
        public mechanicId: string,
        public messages: Message[]
    ) { }
}

在我的Create-Conversation模式中,我通过调用我的Conversation-Service中的方法来创建一个Conversation

 onSendMessage() {
    if (this.form.invalid) {
      return;
    }
    this.conversationsService.addConversation(this.mechanicToContact.id, this.form.value.message);
    this.modalCtrl.dismiss(null, 'cancel');
    this.router.navigateByUrl('conversation-list');
  }

然后我希望用户导航到conversation-list页面,他们应该在其中看到所有现有对话的列表(包括新添加的对话)

这是我的Conversation-Service中的代码:

addConversation(mechanicId: string, message: string) {

    const newConversation = new Conversation(
      Math.random().toString(),
      this.authService.userId,
      mechanicId,
      [this.createMessage(message)]
    );
    this._conversations.push(newConversation);
    console.log(this._conversations);
  }

  private createMessage(message: string): Message {
    return {
      id: Math.random().toString(),
      text: message,
      userId: this.authService.userId,
      timestamp: Date.now().toString()
    };
  }

当我运行此代码时,我正在控制台记录_conversations并看到成功推送了额外的Conversation

但是,当用户导航到Conversation-Detail时,只会显示以前存在的Conversation

有人可以告诉我从Create-Conversation导航到Conversation-Detail时如何显示最新的Conversation对象吗?

这是我的Conversation-Detail页面中的代码:

 ngOnInit() {
    this.loadedConversations = this.conversationsService.conversations;
    console.log(this.loadedConversations);
  }

我设法通过将以下 function 添加到``对话列表:

ionViewWillEnter(){
    this.loadedConversations = this.conversationsService.conversations;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM