我有一个场景,我需要从构造函数和ngOnInit中分派请求操作,并在这两个函数中加载数据。我尝试过,但是在第二次调用中,任何一个调度都没有调用,并且我看到的数据与从上次调用中获得的数据相同。我想知道如何实现它
我尝试从我的条件满足的另一个类中调用dispatch。
constructor(
private store: Store<AppState>,
private globalLoginService: GlobalLoginService,
) {
this.filterService.dashboardModified.subscribe(val => {
if (this.filterService.IsdashboardModified === "true") {
this.getProfileDatafromStore();
}
});
}
public getProfileDatafromStore() {
this.store.dispatch(
new GetUserProfilesRequested(userEmail)
);
this.userProfiles$ = this.store.select(getUserProfile);
this.userProfiles$
.pipe(takeUntil(this.unsubscribe$))
.subscribe(storeval => {
if (storeval) {
this.userProfileData = storeval
this.loginProfile(this.userProfileData);
}
});
}
public ngOnInit() {
this.getProfileDatafromStore();
}
我希望每次从ngOnIt和构造函数进行调用时都加载数据
答案 0 :(得分:0)
您不必创建每个订阅,只需调度一次操作即可,而不必每次getProfileDataFromStore
函数都重新订阅。
constructor(
private store: Store < AppState >,
private globalLoginService: GlobalLoginService,
) {
this.setUpgetProfileDatafromStore(); // <-- setup observables before anything happens
this.filterService.dashboardModified.subscribe(val => {
if (this.filterService.IsdashboardModified === "true") {
this.getProfileDatafromStore();
}
});
}
public setUpgetProfileDatafromStore(){
this.userProfiles$ = this.store.select(getUserProfile);
this.userProfiles$
.pipe(takeUntil(this.unsubscribe$))
.subscribe(storeval => {
if (storeval) {
this.userProfileData = storeval
this.loginProfile(this.userProfileData);
}
});
}
public getProfileDatafromStore() {
this.store.dispatch(
new GetUserProfilesRequested(userEmail)
);
}
public ngOnInit() {
this.getProfileDatafromStore();
}