【发布时间】:2019-04-27 02:27:07
【问题描述】:
这是我在 Angular 中遇到的问题。下面的方法进行了两次 http 调用。
submit(username: string, pword: string) {
this.loginService.signIn(username, pword);
this.loginService.currentloginAttemp.subscribe(data => {
if (data == true) {
this.profileService.getProfile();
}
});
this.profileService.newProfileObj.subscribe(dataProfile =>{
console.log(dataProfile);
});
}
submit(username: string, pword: string) {
this.loginService.signIn(username, pword);
signIn 进行 http 调用并分配给一个名为 currentloginAttemp 的可观察对象
this.loginService.currentloginAttemp.subscribe(data => {
if (data == true) {
this.profileService.getProfile();
}
});
上面的代码没有任何问题。 this.profileService.getProfile() 进行 http 调用以获取新的配置文件。下面的代码订阅了获得的配置文件。
this.profileService.newProfileObj.subscribe(dataProfile =>{
console.log(dataProfile);
});
}
当我执行 console.log(dataProfile);我收到一个未定义的警告。
当我运行这个函数时:
logout() {
this.profileService.newProfileObj.subscribe(dataProfile =>{
console.log(dataProfile);
});
}
console.log(dataProfile) 返回一个配置文件。
如何在 this.profileService.getProfile() 代码行之后延迟,以便当我订阅配置文件时,我的组件会看到它?
编辑:
只是为了清楚。 currentloginAttemp 在 this.loginService.signIn 进行 http 调用时分配。这就是为什么我不确定为什么 newProfileObj 在 this.profileService.getProfile() 方法中分配时为空。
编辑 2:
getProfile() {
let tempURL = this.getProfileUrl + this.currProfileId.value;
this.http.get<Profile>(tempURL).subscribe((data) => {
this.currProfileObj.next(data);
});
}
【问题讨论】:
-
你能在stackbitz上发布一个可测试的代码,以便我们测试它
-
JD333 - 你解决了吗?
标签: angular typescript behaviorsubject