EmberJS - 元件生命週期



元件的生命週期包含根據渲染場景執行的三種方法。

在初始渲染時

  • init
  • didReceiveAttrs
  • willRender
  • didInsertElement
  • didRender

在重新渲染時

  • didUpdateAttrs
  • didReceiveAttrs
  • willUpdate
  • willRender
  • didUpdate
  • didRender

元件銷燬時

  • willDestroyElement
  • willClearRender
  • didDestroyElement

下表列出了使用元件內生命週期鉤子的不同方式 -

序號 生命週期方式和說明
1 使用 didUpdateAttrs 對屬性進行更改

didUpdateAttrs 鉤子可在元件的屬性發生更改且在重新渲染元件前被呼叫時使用。

2 使用 didReceiveAttrs 對屬性進行更新

didReceiveAttrs 鉤子可在 init 方法後使用,且在更新元件的屬性時呼叫。

3 使用 didInsertElement 對第三方庫進行使用

你可以使用此鉤子在 DOM 元素中初始化並附加第三方庫。

4 didRender 和呈現的 DOM

didRender 鉤子在模板渲染時被呼叫以更新 DOM。

5 willDestroyElement 與分離和拆除

你可以透過觸發 willDestroyElement 鉤子從 DOM 中移除元件元素。

emberjs_component.htm
廣告
© . All rights reserved.