角度订阅在局部变量中未定义

阿维纳什·拜利哈尔

我有一个局部变量,并且想在我们的 Angular 组件中使用 Observable 来订阅它并在本地存储响应,但是它总是未定义的一些原因。任何线索为什么不分配?

export class ScheduleHistoryComponent implements OnInit, OnDestroy {

    subscription: Subscription = new Subscription();
    toAccountListDto: any;

    constructor(public paymentService: PaymentService) {}

    ngOnInit(): void {
        this.subscription.add(this.paymentService.getLiteAccountsList()
            .subscribe((res: any) => {
                this.toAccountListDto = (res.data.accountDtoList);
                this.toAccountListDto = this.toAccountListDto.filter(account => account.accountSource.toUpperCase() === 'DDA' ||
                    account.accountSource.toUpperCase() === 'SVG');
                console.log('Inside', this.toAccountListDto); < -- --its prints output here
            }, error => {
                console.error();
            })
        );
        console.log('Outside', this.toAccountListDto); < -- --its prints output as undefined
    }

    ngOnDestroy(): void {
        this.subscription.unsubscribe();
    }

}
质量

试试看,

subscription;

ngOnInit(): void {
    this.subscription = this.paymentService.getLiteAccountsList().subscribe(...);
}

ngOnDestroy(): void {
  if (this.subscription) {
   this.subscription.unsubscribe();
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章