关于angular2更新时机的一些发现
關(guān)于angular2更新時(shí)機(jī)的一些發(fā)現(xiàn)
引入:
angular2官網(wǎng)關(guān)于管道(Pipe)的介紹中說明,angular2的管道分為pure和impure。
- 非純管道Impure pipes
Angular會(huì)在每個(gè)組件的變更檢測(cè)周期中執(zhí)行非純管道。 非純管道可能會(huì)被調(diào)用很多次,和每個(gè)按鍵或每次鼠標(biāo)移動(dòng)一樣頻繁。
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只有在它檢測(cè)到輸入值發(fā)生了純變更時(shí)才會(huì)執(zhí)行純管道。 純變更是指對(duì)原始類型值(String、Number、Boolean、Symbol)的更改, 或者對(duì)對(duì)象引用(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會(huì)忽略(復(fù)合)對(duì)象內(nèi)部的更改。 如果我們更改了輸入日期(Date)中的月份、往一個(gè)輸入數(shù)組(Array)中添加新值或者更新了一個(gè)輸入對(duì)象(Object)的屬性,Angular都不會(huì)調(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ā)過程中,我們遇到過,一個(gè)雙向綁定的數(shù)組,使用push或者splice函數(shù)時(shí),畫面并未發(fā)生刷新。目前看了正是由于類似的情況
解決方法:
第一種:
新建一個(gè)數(shù)組,重新賦值給雙向綁定的變量,這樣,相當(dāng)于引用發(fā)生了變化,所以會(huì)更新模板。
第二種:
使用ngIf來切換模板文件中的代碼段(可能是一個(gè)組件)。
第一種是方法性能應(yīng)該更好。如果這種情況下,也有一個(gè)類似管道中pure的參數(shù)來設(shè)置就好了,目前還沒發(fā)現(xiàn)其他方法。
總結(jié)
以上是生活随笔為你收集整理的关于angular2更新时机的一些发现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网易新闻app闪退(网易游戏官网)
- 下一篇: Chrome 键盘快捷键(Mac)