Aurelia教程

Aurelia 组件生命周期

Aurelia 使用组件生命周期方法来操作组件生命周期。在本章中,我们将向您展示这些方法并解释组件生命周期。
constructor()-构造方法用于初始化用类创建的对象。首先调用此方法。如果不指定此方法,将使用默认构造函数。 created(owningView, myView)-一旦视图和视图模型被创建并连接到控制器,就会调用它。这个方法有两个参数。第一个是声明组件的视图(owningView)。第二个是组件视图(myView) bind(bindingContext, overrideContext)-此时,绑定已经开始。第一个参数表示组件的绑定上下文。第二个是overrideContext。此参数用于添加其他上下文属性。 attached()-一旦组件附加到 DOM,附加方法就会被调用。 detached()-此方法与 attached 相反。当组件从 DOM 中移除时会调用它。 unbind()-最后一个生命周期方法是unbind。当组件未绑定时调用它。
当您希望对组件进行更高的控制时,生命周期方法非常有用。当您需要在组件生命周期的某个时刻触发某些功能时,您可以使用它们。
所有生命周期方法如下所示。

app.js

export class App {
   constructor(argument) {
      // Create and initialize your class object here...
   }
   created(owningView, myView) {
      // Invoked once the component is created...
   }
   bind(bindingContext, overrideContext) {
      // Invoked once the databinding is activated...
   }
   attached(argument) {
      // Invoked once the component is attached to the DOM...
   }
   detached(argument) {
      // Invoked when component is detached from the dom
   }
   unbind(argument) {
      // Invoked when component is unbound...
   }
}
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4