Javascript 组件之间的角度等待服务,行为主体
我的Angular web store出现问题当我刷新窗口时,我创建了一个服务,从服务器获取用户数据,然后使用BehaviorSubject注入到“产品”部分,我的目标是只向服务器发出一个请求:Javascript 组件之间的角度等待服务,行为主体,javascript,angular,Javascript,Angular,我的Angular web store出现问题当我刷新窗口时,我创建了一个服务,从服务器获取用户数据,然后使用BehaviorSubject注入到“产品”部分,我的目标是只向服务器发出一个请求: 从'@angular/core'导入{Injectable}; 从'rxjs/BehaviorSubject'导入{BehaviorSubject}; @注射的({ providedIn:'根' }) 导出类数据服务{ private userId=new BehaviorSubject(“”); c
从'@angular/core'导入{Injectable};
从'rxjs/BehaviorSubject'导入{BehaviorSubject};
@注射的({
providedIn:'根'
})
导出类数据服务{
private userId=new BehaviorSubject(“”);
currentUserId=this.userId.asObservable();
构造函数(){}
sendUserId(消息:字符串){
this.userId.next(消息)
}
}
将您的所有逻辑放在观察者的下一个功能中如何,如下所示:
this.\u dataService.currentUserId.subscribe(userId=>{
如果(userId.length==0)
{
此.userService.getUserProfile()为.subscribe(
res=>{
this.userDetails=res['user'];
this.userId=this.userDetails.\u id;
这个.getProducts();
},
错误=>{
控制台日志(err);
}
);
}否则
{
这个.getProducts();
}
});
看看我的库ez state我还没有为它编写任何文档,但这篇关于它所取代的库的文章对概念进行了介绍,谢谢!,我去看看