Ember组件生命周期挂钩方法 - 是否强制调用super? [英] Ember component life cycle hook method - Is it mandatory to call super?

查看:378
本文介绍了Ember组件生命周期挂钩方法 - 是否强制调用super?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,一些Component生命周期钩子方法没有 this._super(... arguments),如 didReceiveAttrs didUpdateAttrs didRender didInsertElement willDestroyElement
但我没有遇到任何问题。我应该改变包括还是不包括?



我知道如果我们覆盖框架类的 init ,像 Ember.Component ,请确保在我们的init声明中调用 this._super(... arguments)如果我们没有,Ember可能没有机会进行重要的安装工作,我们会在应用程序中看到奇怪的行为。 ( ref

解决方案

不是强制调用 this._super 始终为每一个生命周期的钩子。然而,有时你需要它,有时你不需要它。由于缺少 _super 调用,因为实际需要导致的错误导致通常难以发现或调试的错误。因此,建议始终称之为保持安全。如果你打电话给它并且不需要它,它什么也不做,所以你没有做任何事情。



我想这是一个额外的代码行,但是它真的没有什么缺点,除了你需要键入它的5秒。



对于相关的对话,请参阅:
不推荐使用Ember.on for Component的生命周期事件。


In my project some of the Component life cycle hook methods don't have this._super(...arguments) such as didReceiveAttrs, didUpdateAttrs, didRender, didInsertElement and willDestroyElement. but I didn't face any issue. should I change to include or not ?

I know if we override init for a framework class like Ember.Component, be sure to call this._super(...arguments) in our init declaration! If we don't, Ember may not have an opportunity to do important setup work, and we'll see strange behavior in your application. (ref)

解决方案

It is not mandatory to call this._super always for every single life cycle hook. However, sometimes you need it and sometimes you don't. Bugs that are caused by the lack of the _super call when it was actually necessary results in bugs that are usually difficult to spot or debug. Therefore, the recommendation is to always call it, to stay safe. If you call it and it isn't needed, it will do nothing, so you lose nothing for doing it.

I guess it is one extra line of code, but it really has no downside, except the 5 seconds that it'll take you to type it.

For a related conversation see: Deprecate the usage of Ember.on for Component's lifecycle events.

这篇关于Ember组件生命周期挂钩方法 - 是否强制调用super?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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