如何访问数据外部可观察到的



如何在this. transactionservice . transactionevent $之外访问或获取this.dealType的值?付费吗?. 我想在外面访问this. dealtype的数据和值,也许有人可以帮助。

它在this. transactionservice . transactionevent $中有值。订阅但我想在外部访问它,我尝试使用控制台日志记录它是空的

谢谢你,谢谢。

#代码
export class DealsApprovalComponent implements OnInit {
dealType: string;
totalDealsForApproval = 0;
constructor(
private dealService: DealService,
private notificationService: NotificationService,
private trasactionService: TransactionService,
private route: Router,
private dealTransactionService: DealTransactionService

) 
{ 
}
ngOnInit(): void {
this.trasactionService.transactionEvent$.subscribe((data) => {
if(data) {
switch (data['operation']) {
case 'deal/deal-type':
this.dealType = data["dealType"];
break;
}
}
},
(error) => {}
);
console.log("this.dealType" , this.dealType)
}

控制台日志显示为空,因为它是在订阅方法中的代码之前首先执行的。如果你需要对"this. dealtype"做一些事情,你可以在分配了它之后创建一个方法,就像这样。

ngOnInit(): void {
this.trasactionService.transactionEvent$.subscribe((data) => {
if(data) {
switch (data['operation']) {
case 'deal/deal-type':
this.dealType = data["dealType"];
this.doSomething();
break;
}
}
},
(error) => {}
);
}
private doSomething() {
console.log(this.dealType);
}

另外,记得在需要时取消订阅以避免内存泄漏。

最新更新