繁体   English   中英

如何在不刷新页面的情况下更新视图中显示的数据?

[英]How to update displayed data in the view, without refreshing the page?

我有一个用例,用户可以在其中收藏一个项目。 下面的代码显示了我如何从数据库中检索数据以显示在视图中。

最喜欢的服务

   async getfavoriteList(): Promise<firebase.firestore.QuerySnapshot> {
    const user: firebase.User = await this.authService.getUser();
    if (user) 
          {
    this.FavoriteListRef = firebase
      .firestore()
      .collection(`userFavorites`);
    return this.FavoriteListRef.where('uid', '==', user.uid).get();
              }
           } 
       }

收藏夹.ts

ngOnInit() {    this.favoriteService.getfavoriteList().then(favoriteListSnapshot => {
      this.favoriteList = [];
      favoriteListSnapshot.forEach(snap => {
        this.favoriteList.push({
          id: snap.id,
          favoriteList: snap.data().favourite
        });
        console.log(snap.data().favourite)
        return false;
      });
    }); 

  }

我遇到的问题是,当用户收藏某个项目并转到收藏页面查看收藏的项目时,除非刷新页面,否则视图不会更新。 这不是理想的用户体验。 如何在不刷新页面的情况下更新视图。 我尝试将代码放在构造函数 function 以及 ngOnInit() 中,但页面没有更新。

我建议您在promise上使用Observable 这将帮助您在不重新加载的情况下获取数据。

参考官方文档

暂无
暂无

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

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