1. 程式人生 > >前端框架Aurelia——元件Component(二)元件生命週期

前端框架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.
意思是初始化的時候屬性被初始化不會呼叫繫結的回撥函式,只有在後面的時候屬性改變才會呼叫回撥。