Aurelia - 组件生命周期

Aurelia使用组件生命周期方法来操纵组件生命周期.在本章中,我们将向您展示这些方法并解释组件生命周期.

  • constructor() : 构造方法用于初始化使用类创建的对象.首先调用此方法.如果你没有指定这个方法,将使用默认的构造函数.

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

  • bind(bindingContext,overrideContext)&minus ;此时,绑定已经开始.第一个参数表示组件的绑定上下文.第二个是 overrideContext .此参数用于添加其他上下文属性.

  • 附加() : 一旦组件附加到DOM,就会调用附加方法.

  • detached() : 此方法与附加相反.从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...
   }
}