微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Angular - 使用查询参数从另一个组件重定向后重新加载/更新组件数据

如何解决Angular - 使用查询参数从另一个组件重定向后重新加载/更新组件数据

我正在处理 2 个组件。 MyNotesComponentCoursetoolsComponent

CoursetoolsComponent 中,我添加一个新笔记并显示最近的笔记,如下所示。

enter image description here

当我点击任何注释时(如图“注释 1、注释 2 或注释 3”所示)。我在 CoursetoolsComponent 中点击笔记有以下功能

directToNote(id){
     this.router.navigate(['/student/my-notes'],{queryParams: {"notes": id}})
    }

这会将我引导至 /student/my-notes?notes=1

问题: 如果我直接打开 MyNotesComponent,它会正确显示获取的笔记数据。查看该数据后,我转到 CoursetoolsComponent添加新注释并单击新添加的注释它会将我重定向MyNotesComponent 页面,但数据未在 MyNotesComponent 上更新添加了最新的注释。 如果我刷新页面,则会更新并显示数据。 每次导航到 MyNotesComponent 时如何刷新数据?

MyNotesComponent.ts

 ngOnInit(){
  this.fetchNotes();
}
 //Get All Notes 
  fetchNotes(){
    this.courseApi.coursesCourseNotesNotesList().subscribe(
      (data: any) => {
        this.notesSaved = data;
      },(err) => { console.error(err); },() => { }
    )
  }

我尝试过的事情: - 将 routeReuseStrategy 添加到 CoursetoolsComponent 中的路由导航:

 directToNote(id){
            this.router.routeReuseStrategy.shouldReuseRoute = function () {
              return false;
          }
           this.router.navigate(['/student/my-notes'],{queryParams: {"notes": id}})
          }

不起作用。 还将 routeReuseStrategy 添加MyNotesComponent 的构造函数中,它根本不起作用。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。