简体   繁体   English

为什么我不能从 ngOnInit 方法设置数组实例变量?

[英]Why can't I set an array instance variable from the ngOnInit method?

Why can't I set an array instance variable from the ngOnInit method?为什么我不能从ngOnInit方法设置数组实例变量?

As you see in this screenshot, the last 2 console.log lines attempt to print the array.正如您在此屏幕截图中看到的,最后 2 行console.log尝试打印数组。 The first console.log prints the array with its 1 containing item.第一个console.log打印包含 1 项的数组。 The second console.log outside of the .subscribe method prints an empty array as if the saved array is lost after the scope of the .subscribe method is done. .subscribe方法之外的第二个console.log打印一个空数组,好像保存的数组在.subscribe方法的 scope 完成后丢失了。

When my app loads, the list I am trying to display is empty because the array is empty.当我的应用程序加载时,我试图显示的列表是空的,因为数组是空的。

在此处输入图像描述

Same as this question but NONE of the answers clarify it for me: Initializing properties inside ngOnInit scope与此问题相同,但没有一个答案为我澄清: Initializing properties inside ngOnInit scope

It helps to know that the callback functions are not executed where they are declared.知道回调函数没有在声明它们的地方执行会很有帮助。 Angular manages the Observables outside the context of your component. Angular 在组件上下文之外管理Observable The callback function in the subscribe function declared there in the ngOnInit will be run by angular every time the observable returned from the getChannelStatsById() function emits a value. The callback function in the subscribe function declared there in the ngOnInit will be run by angular every time the observable returned from the getChannelStatsById() function emits a value. Even after the ngOnInit has long completed.即使在 ngOnInit 早已完成之后。

When the callback function executes, the rowData value will be set and the HTML template should update automatically.当回调 function 执行时, rowData值将被设置并且 HTML 模板应该自动更新。 An *ngIf check can also be used to see if you have values before displaying the data *ngIf检查也可用于在显示数据之前查看您是否有值

<ng-container *ngIf="rowData?.length > 0">
  {{ rowData }}
</ng-container>

I am assuming the getChannelStatsById() function makes an HTTP call in which case it returns an observable that will most probably emit only one value on a successful response from the HTTP call.我假设getChannelStatsById() function 进行了 HTTP 调用,在这种情况下,它返回一个可观察的值,很可能在 Z293C9EA246FF9985DC6F62A650F 调用的成功响应中只发出一个值。 But it is possible to configure http calls to emit multiple values too.但是也可以配置 http 调用来发出多个值。 The flow of execution in the component creation or the lifecycle hook like the ngOnInit function does not stop for the observable to emit a value.组件创建或生命周期钩子(如 ngOnInit function)中的执行流程不会因为 observable 发出值而停止。

Also, very important to remember is observables can emit values indefinitely and angular may keep executing those callback functions after the component has been destroyed.此外,非常重要的是要记住 observables 可以无限期地发出值,并且 angular 可能会在组件被销毁后继续执行这些回调函数。 Using async pipe in the HTML template would make life easy as angular handles the subscription OR unsubscribing in the ngOnDestroy lifecycle hook.在 HTML 模板中使用异步pipe 将使生活变得轻松,因为 angular 在 ngOnDestroy 生命周期挂钩中处理订阅或取消订阅。

The method subscribes to an observable and essentially asks for a result, then it moves on and prints an initially empty array, THEN the result comes back and the array gets the result pushed to it, as seen in the following console.log .该方法订阅一个 observable 并且本质上要求一个结果,然后它继续并打印一个最初为空的数组,然后返回结果并将结果推送给它,如下面的console.log所示。 This is just how asynchronous javascript works.这就是异步 javascript 的工作原理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM