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.