角度4-是否可以在不触发OnInit的情况下运行OnDestroy? [英] Angular 4 - Can OnDestroy be run without the OnInit ever being triggered?

查看:105
本文介绍了角度4-是否可以在不触发OnInit的情况下运行OnDestroy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于两个生命周期挂钩OnInitOnDestroy的一般问题.因此条提到,我一直认为OnInit始终在OnDestroy之前运行 .

Just a generic question regarding the two Lifecycle Hooks OnInit and OnDestroy. As this article mentions, I had always assumed that OnInit is always run before the OnDestroy.

我遇到了在ngOnDestroy()中停止播放背景音乐曲目的情况.此声音已加载到组件的ngOnInit(),并且由于正在运行ngOnDestroy()而未运行ngOnInit(),因此声音对象是undefined.

I have a case where in an ngOnDestroy() I'm stopping a background music track from playing. 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() {
    ...

    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);
}

目前,当尝试.fade()声音为undefined时,代码在ngOnDestroy中失败.当然,我可以通过在执行.fade()功能之前检查声音是否不是undefined来轻松解决此问题.我的假设是,如果运行ngOnDestroy(),那么ngOnInit()也必须也运行过-我想我错了.

At the moment the code is failing in the ngOnDestroy when trying to .fade() a sound that's undefined. Of course I can easily fix this by checking that the sound is not undefined before executing the .fade() function. My assumption was that if an ngOnDestroy() is run the ngOnInit() must have been run as well - I guess I was wrong.

现在由于这种情况,我想在我的应用程序中的每个ngOnDestroy()中,我应该在执行任何操作之前检查使用的对象是否为undefined.因此,例如,在取消订阅之前,我应该检查订阅是否为undefined,依此类推.

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. So for example before unsubscribing from a subscription I should check if the subscription is undefined, and so on.

我可以这样假设吗?

推荐答案

参考清楚指出OnInit生命周期挂钩在OnDestroy之前.通常,此行为特定于编译器. 文章解释了特定于Angular路由器的行为以及<router-outlet>指令实例化组件的方式.路径组件构造函数中的router.navigateByUrl()

The reference clearly states that OnInit lifecycle hook precedes 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. router.navigateByUrl() in route component constructor triggers its immediate destruction and prevents it from being compiled, so the lifecycle isn't applicable to it. This won't happen under normal circumstances.

问题中的代码并不表示ngOnInit不运行.可以轻松记录ngOnInit调用,以证明其运行.

The code in the question doesn't imply that ngOnInit doesn't run. ngOnInit call be easily logged to prove that it runs.

此组件使用的服务可能会导致此问题.在ngOnDestroy中抛出无法读取未定义的属性'fade'或类似错误的事实意味着this.soundService.getSound(Sound.MINI_GAME_BG_MUSIC)是未定义的.

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.

这完全取决于soundService服务内部发生的情况.如果loadSound是异步的,并且ngOnDestroy在加载资源之前被调用,则可能有问题.

This totally depends on what happens inside soundService service. If loadSound is asynchronous, and ngOnDestroy was called before a resource was loaded, there may be problems.

这篇关于角度4-是否可以在不触发OnInit的情况下运行OnDestroy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆