【发布时间】:2020-09-21 19:01:18
【问题描述】:
我有一个允许用户编辑条目的组件。如果用户从前一个组件(表)到达编辑页面,我的观察者能够从 Store 中选择用户并使用正确的数据填充字段。
但是,如果我刷新页面,观察者确实会继续返回 null。奇怪的是,编辑表单上的 nGif 填写了用户的正确标签。这告诉我 Observable 对象不为空,因为 nGif 不会呈现标签。订阅它的观察者似乎是问题所在。
表格:
<form id="editContainer" [formGroup]="editForm" *ngIf="userObjectUnderEdit | async as userEdit">
<div class="form-group">
<mat-form-field appearance="standard">
<mat-label>{{userEdit.name}}</mat-label>
<input class="formInput" matInput placeholder="{{userEdit.name}}" formControlName="name" required>
</mat-form-field>
</div>
<div class="form-group">
<mat-form-field appearance="standard">
<mat-label>{{userEdit.email}}</mat-label>
<input class="formInput" resizeToFitContent="true" matInput placeholder="{{userEdit.email}}" formControlName="email" required>
</mat-form-field>
</div>
...
组件:
ngOnInit() {
console.log('in ngoninit');
this.editBuildForm();
this.loaduserForEdit();
// this.setEditFormValues();
}
loadUserForEdit() {
const routeSubscription = this.store.select(getCurrentRouteState).pipe(take(1)).subscribe(route => {
const userId = route.params.userId;
console.log("route params id: ", userId);
this.store.dispatch(loadEditeduser({userId}));
this.store.dispatch(setEditeduserId({userId}));
});
this.userObjectUnderEdit = this.store.select(getCurrentuser);
const userFillSubscriber = this.userObjectUnderEdit.subscribe(user => this.userFormFill = user);
this.setEditFormValues(this.userFormFill);
userFillSubscriber.unsubscribe();
routeSubscription.unsubscribe();
}
const userFillSubscriber = this.userObjectUnderEdit.subscribe(user => this.userFormFill = user); ^ 每当页面刷新时,用户为空。 但是,ngIf 仍然会填充 mat-labels,因此 userObjectUnderEdit 仍然可以正常工作。
【问题讨论】:
-
能分享一下重载页面时的状态吗?还有 getCurrentUser 选择器?
-
状态不会在浏览器刷新时持续存在,而是执行 GET 操作以根据路由参数仅检索该用户。
-
我的意思是调用 ngOnInit 的那一刻。但是我看到你的问题已经解决了!祝你好运。
标签: angular rxjs observable ngrx