Angular ChangeDetectorRef

Angular 各類視圖的基礎類,提供變動檢測功能。 變動檢測樹會收集要檢查的全部視圖。 使用這些方法從樹中添加或移除視圖、初始化變動檢測並顯式地把這些視圖標記爲髒的,意思是它們變了、須要從新渲染
參考資料:
深刻 Angular 的 Change Detection 機制
變化檢測策略:OnPush
source code:
`export interface Component extends Directive {優化

/**
 * The change-detection strategy to use for this component.
 *
 * When a component is instantiated, Angular creates a change detector,
 * which is responsible for propagating the component's bindings.
 * The strategy is one of:
 * - ChangeDetectionStrategy#OnPush sets the strategy to CheckOnce (on demand).
 * - ChangeDetectionStrategy#Default sets the strategy to CheckAlways.
 */
changeDetection?: ChangeDetectionStrategy;

當一個組件實例化的時候,ng會建立一個變化檢測器,它給出了兩種選擇,
變化檢測策略:OnPush,當使用 OnPush 策略的時候,若輸入屬性沒有發生變化,組件的變化檢測將會被跳過,按需檢查
默認策略:Default ,一個也不放過,所有弄死,變化檢測器的狀態值是 CheckAlways.`this

當在寫一個組件的時候,使用OnPush + ChangeDetectorRef來優化code

相關文章
相關標籤/搜索