首页 > 解决方案 > 导航/路由回组件时不会触发 ngOnInit

问题描述

我想从服务中检索数据并将其分配给组件以查看和更新​​。它起作用了,但是每当我导航到其他路线并返回时,它就会消失并且一切都未定义。我需要针对这种情况的解决方案。

待办组件

ngOnInit(){
      this.abv = this.memberData.memberData$.subscribe(res => {
        this.todos = res.todos.todo;
        this.done = res.todos.done;
      })
    }

路由模块

const routes: Routes = [
  {path:'', component:MemberComponent, children:[
    {path:'profile', component: ProfileComponent},
    {path:'todo', component: TodoComponent}
  ]}
];

服务


    this.memberData$ = this.memberRef.snapshotChanges()
    .pipe(
        map(action => {
            const data = action.payload.data();
            const id = action.payload.id;
            return { id, ...data };
        })
     );

标签: angularangular-routingngoninit

解决方案


推荐阅读