简体   繁体   English

Angular 4 - 可以在没有触发OnInit的情况下运行OnDestroy吗?

[英]Angular 4 - Can OnDestroy be run without the OnInit ever being triggered?

Just a generic question regarding the two Lifecycle Hooks OnInit and OnDestroy . 只是关于两个生命周期钩子OnInitOnDestroy的一般性问题。 As this article mentions, I had always assumed that OnInit is always run before the OnDestroy . 正如本文所提到的,我一直认为OnInit 总是OnDestroy之前运行。

I have a case where in an ngOnDestroy() I'm stopping a background music track from playing. 我有一个案例,在ngOnDestroy()我停止播放背景音乐曲目。 This sound is loaded the component's ngOnInit() , and because the ngOnDestroy() is being run without the ngOnInit() being run the sound object is undefined . 该声音被加载组件的ngOnInit()并且因为ngOnDestroy()正在不运行ngOnInit()中运行声音对象是undefined

Code

ngOnInit() {
    ...

    this.loadSounds();

    ...
}

ngOnDestroy() {
    if (AppSettings.SOUNDS_ENABLED) {
        this.soundService.getSound(Sound.MINI_GAME_BG_MUSIC).fade(0.2, 0, 1500);
    }
}

private loadSounds() {
    this.soundService.loadSound(Sound.MINI_GAME_BG_MUSIC, SoundPathURL.MINI_GAME_BG_MUSIC, true, 0);
}

At the moment the code is failing in the ngOnDestroy when trying to .fade() a sound that's undefined . 当尝试.fade()声音undefined时,代码在ngOnDestroy失败。 Of course I can easily fix this by checking that the sound is not undefined before executing the .fade() function. 当然,我可以通过在执行.fade()函数之前检查声音是否undefined来轻松解决此问题。 My assumption was that if an ngOnDestroy() is run the ngOnInit() must have been run as well - I guess I was wrong. 我的假设是,如果一个ngOnDestroy()在运行ngOnInit()必须已经在运行,以及-我想我错了。

Now because of this case I'm thinking that in every ngOnDestroy() in my application I should check whether the object being used is undefined before performing any operations. 现在因为这种情况我想在我的应用程序中的每个ngOnDestroy()中,我应该在执行任何操作之前检查所使用的对象是否是undefined So for example before unsubscribing from a subscription I should check if the subscription is undefined , and so on. 因此,例如在取消订阅订阅之前,我应该检查订阅是否undefined ,依此类推。

Am I right to assume so? 我这样做是对的吗?

The reference clearly states that OnInit lifecycle hook precedes OnDestroy . 该引用明确指出OnInit生命周期钩子在OnDestroy之前。 This behaviour is specific to the compiler in general. 此行为通常特定于编译器。 The article explains the behaviour that is specific to Angular router and the way <router-outlet> directive instantiates components. 本文解释了Angular路由器特有的行为以及<router-outlet>指令实例化组件的方式。 router.navigateByUrl() in route component constructor triggers its immediate destruction and prevents it from being compiled, so the lifecycle isn't applicable to it. 路由组件构造函数中的router.navigateByUrl() 触发其立即销毁并阻止其被编译,因此生命周期不适用于它。 This won't happen under normal circumstances. 在正常情况下不会发生这种情况。

The code in the question doesn't imply that ngOnInit doesn't run. 问题中的代码并不意味着ngOnInit不会运行。 ngOnInit call be easily logged to prove that it runs. 可以轻松记录ngOnInit调用以证明它运行。

The service that is used by this component can cause the problem. 此组件使用的服务可能会导致该问题。 The fact that Cannot read property 'fade' of undefined or similar error is thrown in ngOnDestroy means that this.soundService.getSound(Sound.MINI_GAME_BG_MUSIC) is undefined. ngOnDestroy抛出无法读取未定义或类似错误的属性“淡入淡出”的 ngOnDestroy意味着未定义this.soundService.getSound(Sound.MINI_GAME_BG_MUSIC)

This totally depends on what happens inside soundService service. 这完全取决于soundService服务中发生的事情。 If loadSound is asynchronous, and ngOnDestroy was called before a resource was loaded, there may be problems. 如果loadSound是异步的,并且在加载资源之前调用了ngOnDestroy ,则可能存在问题。

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

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