前端框架Aurelia——組件Component(二)組件生命週期

  1. constructor() - The view-model's constructor is called first.
  2. created(owningView: View, myView: View)
The created callback will receive the instance of the "owningView".
This is the view that the component is declared inside of.
If the component itself has a view, this will be passed second.
owningView是這個組件要在哪個View裏面渲染,myView是自己的view。如果有自己的view,這個owningView
就會被變成第二順位。

3.bind(bindingContext: Object, overrideContext: Object)
If the view-model has a bind callback, it will be invoked at this time. 
The "binding context" to which the component is being bound will be passed first.
An "override context" will be passed second. 

4. attached() - Next, the component is attached to the DOM (in document).
If the view-model has an attached callback, it will be invoked at this time
組件添加到DOM上

5.detached()  At some point in the future, the component may be removed from the DOM.
If/When this happens, and if the view-model has a detached callback, this is when it will be invoked.
如果組件被從DOM上移出且組件有這個方法,那麼這個方法將被調用。

6.unbind() - After a component is detached, it's usually unbound.
If your view-model has the unbind callback, it will be invoked during this process.
detached()方法後纔是unbind()方法。先分離後取消綁定。
unbind會直接開始改變view,detached肯定在unbind前面,首先要從DOM上面移出。

Each of these callbacks is optional.
這些回調都是可選的。
if you implement bind you will need to implement unbind.
The same goes for attached and detached, but again, it isn't mandatory.
通常。bind和unbind(), attached和detached方法是一對寫入,但不是強制。

It is important to note that if you implement the bind callback function,
then the property changed callbacks for any bindable properties will not be called when the property value is initially set.
The changed callback will be called for any subsequent time the bound value changes.

意思是初始化的時候屬性被初始化不會調用綁定的回調函數,只有在後面的時候屬性改變纔會調用回調。

發佈了102 篇原創文章 · 獲贊 56 · 訪問量 42萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章