等待方法执行,然后在Angular中执行下一个代码
我正在尝试在订阅方法之外使用requestDetails变量,但它显示“未定义”
服务
requestDetails$ = new Subject<any>();
updateApprovalMessage(message: string) {
this.requestDetails$.next(message)
}
。TScomponent.ts
requestDetails:any
this.technicalRequestService.requestDetails$.subscribe(data=>{
this.requestDetails=data
console.log(this.requestDetails)
//here it is printing
}
console.log(this.requestDetails)
//but its is showing undefined
I am trying to use the requestDetails variable outside the subscribe method but its showing "undefined"
service.ts
requestDetails$ = new Subject<any>();
updateApprovalMessage(message: string) {
this.requestDetails$.next(message)
}
component.ts
requestDetails:any
this.technicalRequestService.requestDetails$.subscribe(data=>{
this.requestDetails=data
console.log(this.requestDetails)
//here it is printing
}
console.log(this.requestDetails)
//but its is showing undefined
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,首先记录的不确定是如何的?
那是因为您的回调函数尚未被调用。如果要在
this.requestdetails = Data
之后执行代码,则必须在功能中进行。您当然也可以从这里调用其他方法。或者,如果您喜欢
async
/等待
,但这只会触发一次,对单个请求有用,但无用。
Notice how undefined is logged first?
That's because your callback function hasn't been called yet. If you want to execute code after
this.requestDetails=data
, you will have to do so inside the function. You can of course call other methods from here as well.Or if you prefer
async
/await
But this will only fire once, useful for single requests but not data streams.