Aurelia - 组件生命周期



Aurelia 使用组件生命周期方法来操作组件的生命周期。本章将向您展示这些方法并解释组件的生命周期。

  • constructor() − 构造函数用于初始化用类创建的对象。此方法首先被调用。如果您没有指定此方法,则将使用默认构造函数。

  • created(owningView, myView) − 一旦视图和视图模型创建并连接到控制器,就会调用此方法。此方法接受两个参数。第一个参数是声明组件的视图(owningView)。第二个是组件视图(myView)

  • bind(bindingContext, overrideContext) − 在这一点上,绑定已经开始。第一个参数表示组件的绑定上下文。第二个是overrideContext。此参数用于添加其他上下文属性。

  • attached() − 一旦组件附加到 DOM,就会调用 attached 方法。

  • 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...
   }
}
广告
© . All rights reserved.