javascript Aurelia 类构造函数与激活

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31733341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:17:07  来源:igfitidea点击:

Aurelia class Constructor vs Activate

javascriptconstructoraurelia

提问by Daniel Billingham

When exporting an es6 class, which is acting as a view model in aurelia, I seem to be able to set initialization code in both the constructor and an activate function.

当导出一个 es6 类时,它在 aurelia 中充当视图模型,我似乎能够在构造函数和激活函数中设置初始化代码。

Are there any standard conventions here?

这里有任何标准约定吗?

Should I be doing certain initialization in one but not the other?

我应该在一个而不是另一个中进行某些初始化吗?

Is the activate function there for users not implementing es6 classes?

没有实现 es6 类的用户是否有激活功能?

回答by dfsq

You can set instance properties in both constructor and activate methods, they are both going to be invoked by Aurelia. However, there is sort of conceptual difference here.

您可以在构造函数和激活方法中设置实例属性,它们都将被 Aurelia 调用。但是,这里存在某种概念上的差异。

Activate is one of the screen activation lifecycle methods and should ideally be used to control screen/view-model behavior only. For example, canDeactivatemethod controls whether view-model can be navigated to, etc. Activate is also a hook which is executed just before view-model gets rendered (but before attachedhook). However, it is possible that activatemethod will never be called is say route navigates away in constructor or canActivatemethods rejects/returns false - in this case construct will still be invoked, but activate will be not.

Activate 是屏幕激活生命周期方法之一,最好仅用于控制屏幕/视图模型行为。例如,canDeactivate方法控制是否可以导航到视图模型等。 Activate 也是一个钩子,它在视图模型被渲染之前(但在attached钩子之前)执行。但是,有可能activate永远不会调用方法,例如路由在构造函数中导航离开或canActivate方法拒绝/返回 false - 在这种情况下,构造仍将被调用,但 activate 将不会被调用。

On the other hand, constructmethod is invoked before any other hooks and methods, so it's called before activate. For this reason, construct is a primary place for setting configuration properties because it is takes dependency injections. So while activate takes fixed set of parameters (params, routeConfig, navigationInstruction), parameters list passed to constructormethod depends on what services you inject into your view-model class.

另一方面,constructmethod 在任何其他钩子和方法之前被调用,所以它被调用 before activate。出于这个原因,construct 是设置配置属性的主要地方,因为它需要依赖注入。因此,虽然 activate 接受一组固定的参数(params、routeConfig、navigationInstruction),但传递给constructor方法的参数列表取决于您注入到视图模型类中的服务。

回答by Lukas K

One big difference I see here is that activate method has a Promis as return value so you can run here async code. Triggering async code in constructor is a very bad idea. Further detail is that constructor must not throw an exception so typically here you just assign constructor parameters to local variables without anylogic. I would not do more stuff in constructor and the actual viewmodel initialization with logic should happen in activateor attachedmethod.

我在这里看到的一个很大的区别是 activate 方法有一个 Promis 作为返回值,所以你可以在这里运行异步代码。在构造函数中触发异步代码是一个非常糟糕的主意。进一步的细节是构造函数不能抛出异常,所以通常在这里你只需将构造函数参数分配给局部变量而没有任何逻辑。我不会在构造函数中做更多的事情,并且实际的带有逻辑的视图模型初始化应该发生在activate附加方法中。