angular - 导航/路由回组件时不会触发 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 };
})
);
解决方案
推荐阅读
- c# - 有没有比“一劳永逸”更好、更可靠的模式来同时处理可变数量的异步任务?
- sql - Redshift无法将字符串转换为日期,尝试了多种功能
- html - 如何将 3 个按钮与每个表单对齐?
- java - 使用 java 信号量作为两个 Runnable 类之间的锁
- firebase - 我无法在颤振中使用火力基础数据库更新我的产品
- azure - 每当有 azure keyvault 机密更改时,如何触发 Azure devops 管道?
- dart - 如何描述飞镖中没有价值?
- react-native - 将 React Native 的 FlatList 项目的容器与页眉/页脚分开?
- javascript - roundslider js没有正确呈现html2canvas
- c# - 模型对控制器为空