目录

Aurelia - 组件生命周期( Component Lifecycle)

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...
   }
}
↑回到顶部↑
WIKI教程 @2018