订阅最初工作正常,但在Angular应用程序上刷新页面时不返回任何值

订阅最初工作正常,但在Angular应用程序上刷新页面时不返回任何值,angular,typescript,Angular,Typescript,当我在Angular应用程序中导航到某个页面时,我使用以下方法按预期显示数据: conversationsSub: Subscription; usersSub: Subscription; conversation: Conversation; loadedMessages: Message[]; ionViewWillEnter() { this.conversationsService.fetchConversation(this.conversationId).subscribe();

当我在Angular应用程序中导航到某个页面时,我使用以下方法按预期显示数据:

conversationsSub: Subscription;
usersSub: Subscription;
conversation: Conversation;
loadedMessages: Message[];

ionViewWillEnter() {
this.conversationsService.fetchConversation(this.conversationId).subscribe();
}

loadMsg() {
    this.route.paramMap.subscribe(paramMap => {
    this.conversationId = paramMap.get('conversationId');
      this.conversationsSub = this.conversationsService
        .getConversation(paramMap.get('conversationId'))
        .subscribe(conversation => {
          console.log('Conversation values: ', this.conversation);
    });
  }

ionViewWillEnter() {  
    this.loadMsg();
}
以下是一些对话。服务代码,如果需要,我可以提供更多:

private _conversations = new ReplaySubject<Conversation[]>(1);

get conversations() {
    return this._conversations.asObservable();
}

 getConversation(id: string) {
    return this.conversations.pipe(
      take(1),
      map(conversations => {
        return { ...conversations.find(conversation => conversation.id === id) };
      }));
  }

fetchConversation(id: String) {
    return this.http
      .get<{ [key: string]: ConversationData }>('myUrl/conversations.json')
      .pipe(map(resData => {
        const conversations = [];
        for (const key in resData) {
          if (resData.hasOwnProperty(key)) {
            conversations.push(
              new Conversation(
                key,
                resData[key].userId,
                resData[key].mechanicId,
                resData[key].messages
              ));
          }
        }
        console.log('Service Conversation ', { ...conversations.find(conversation => conversation.id === id) });
        return { ...conversations.find(conversation => conversation.id === id) };
      }),
        tap(conversations => {
          this._conversations.next(conversations); // not sure what this should do or does
        })
      );
  }
private\u conversations=新的ReplaySubject(1);
获取对话(){
返回此内容。_conversations.asObservable();
}
getConversation(id:string){
归还这个.conversations.pipe(
以(1)为例,
映射(对话=>{
返回{…conversations.find(conversation=>conversation.id==id)};
}));
}
fetchConversation(id:String){
返回此文件。http
.get('myUrl/conversations.json')
.pipe(映射(resData=>{
const conversations=[];
for(resData中的常量键){
if(resData.hasOwnProperty(键)){
对话。推(
新对话(
钥匙
resData[key].userId,
resData[key].mechanicId,
resData[key]。消息
));
}
}
log('Service Conversation',{…conversations.find(Conversation=>Conversation.id==id)});
返回{…conversations.find(conversation=>conversation.id==id)};
}),
点击(对话=>{
this._conversations.next(conversations);//不确定这应该做什么或做什么
})
);
}
但是,当我刷新页面时,发生了以下情况:

  • 页面上未显示任何数据
  • console.log('Conversation values:',this.Conversation)为空
  • 我还收到此控制台错误
    this.conversation.messages不可编辑
  • 上述错误与
    this.loadedMessages=[…this.conversation.messages]有关在我的
    loadMsg()中

    有人能告诉我为什么在我第一次导航到该页面时这很好,但在我重新加载时会导致问题吗?

    2问题

  • fetchConversations
    如果它被列表页面调用,那么它似乎从未被调用过,并且这些列表页面没有加载到直接消息导航中。您需要在加载的页面中的某个位置调用它才能运行

  • 修复该问题后,您只需从BehaviorSubject获取初始空数组值,而不是获取的值,因为您的组件在获取完成之前订阅,请改用ReplaySubject:

    private\u conversations=新的ReplaySubject(1)


  • 重播主题与行为主题类似,它们缓存大量值并向新订阅者发出,但它们没有初始值,只有在有值要缓存时才会发出。

    因为它在导航时有效,但在重新加载时无效,我认为问题在于路线的paramMap似乎在推送不同的东西(空/未定义)。重新加载时,url参数是否仍在url中?您可以通过添加一个tap并记录来检查它:this.route.paramMap.pipe(tap(console.log)).subscribe(…)我在
    loadMsg()
    中记录了paramMap&记录了正确的值。即使在重新加载时,它也会出现在URL中。这是什么?您的服务中的对话以及它是如何加载的。问题是,当重新加载和导航时,它似乎包含了一些不同的内容。谢谢您的关注。我用更多的服务代码更新了我的问题。如果需要更多,请告诉我,或者我需要解释什么叫
    fetchConversations
    ?它是在重新加载的情况下被调用的吗?谢谢你的回答。我应该在这里使用
    fetchConversations
    ,还是尝试创建一个使用paramMap值的新的
    fetchConversations
    方法?取决于应用程序的需要我已经在上面的答案中添加了一个
    fetchConversations
    方法,并更新了
    \u conversations
    。同时更新了
    ionViewWIllEnter
    ,但是消息仍然没有显示您现在把所有的时间都搞乱了,看起来您已经破坏了类型。如果您只是在条目上获取对话,那么jsut使用fetch方法。或者使用fetchall方法