关于angular2更新时机的一些发现
關(guān)于angular2更新時機(jī)的一些發(fā)現(xiàn)
引入:
angular2官網(wǎng)關(guān)于管道(Pipe)的介紹中說明,angular2的管道分為pure和impure。
- 非純管道Impure pipes
Angular會在每個組件的變更檢測周期中執(zhí)行非純管道。 非純管道可能會被調(diào)用很多次,和每個按鍵或每次鼠標(biāo)移動一樣頻繁。
Angular executes an impure pipe during every component change detection cycle. An impure pipe will be called a lot, as often as every keystroke or mouse-move.
- 純管道Pure pipes
Angular只有在它檢測到輸入值發(fā)生了純變更時才會執(zhí)行純管道。 純變更是指對原始類型值(String、Number、Boolean、Symbol)的更改, 或者對對象引用(Date、Array、Function、Object)的更改。
Angular executes a pure pipe only when it detects a pure change to the input value. A pure change is either a change to a primitive input value (String, Number, Boolean, Symbol) or a changed object reference (Date, Array, Function, Object).
Angular會忽略(復(fù)合)對象內(nèi)部的更改。 如果我們更改了輸入日期(Date)中的月份、往一個輸入數(shù)組(Array)中添加新值或者更新了一個輸入對象(Object)的屬性,Angular都不會調(diào)用純管道。
Angular ignores changes within (composite) objects. It won’t call a pure pipe if we change an input month, add to an input array, or update an input object property.
具體問題:
在以前的開發(fā)過程中,我們遇到過,一個雙向綁定的數(shù)組,使用push或者splice函數(shù)時,畫面并未發(fā)生刷新。目前看了正是由于類似的情況
解決方法:
第一種:
新建一個數(shù)組,重新賦值給雙向綁定的變量,這樣,相當(dāng)于引用發(fā)生了變化,所以會更新模板。
第二種:
使用ngIf來切換模板文件中的代碼段(可能是一個組件)。
第一種是方法性能應(yīng)該更好。如果這種情況下,也有一個類似管道中pure的參數(shù)來設(shè)置就好了,目前還沒發(fā)現(xiàn)其他方法。
總結(jié)
以上是生活随笔為你收集整理的关于angular2更新时机的一些发现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网易新闻app闪退(网易游戏官网)
- 下一篇: dnf雪人套怎么获得 地下城与勇士