变化检测

        When you pass a plain JavaScript object to a Vue instance as its data option, Vue will walk through all of its properties and convert them to getter/setters using Object.defineProperty. This is an ES5-only and un-shimmable feature, which is why Vue doesn’t support IE8 and below.

        The getter/setters are invisible to the user, but under the hood they enable Vue to perform dependency-tracking and change-notification when properties are accessed or modified. One caveat is that browser consoles format getter/setters differently when converted data objects are logged, so you may want to install vue-devtools for a more inspection-friendly interface.

        Every component instance has a corresponding watcher instance, which records any properties “touched” during the component’s render as dependencies. Later on when a dependency’s setter is triggered, it notifies the watcher, which in turn causes the component to re-render.


Change Detection Caveats

Adding new properties to an object

Setting items on an array

Setting the length of an array

        Due to the limitations of modern JavaScript (and the abandonment of Object.observe), Vue cannot detect property addition or deletion

        Vue does not allow dynamically adding new root-level reactive properties to an already created instance. However, it’s possible to add reactive properties to a nested object using the Vue.set(object, key, value) method:

        Sometimes you may want to assign a number of properties to an existing object, for example using Object.assign() or _.extend(). However, new properties added to the object will not trigger changes. In such cases, create a fresh object with properties from both the original object and the mixin object

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,419评论 0 10
  • 36/16000,31/35 1各种一元捐。 2陪先生看病一天。 3今天微商卖货给孤儿院捐10元。
    南戴河西谜会馆慧慧阅读 149评论 0 0
  • connect 控件到槽 清除 QMessageBox toInt() 控件链接到槽的四种方式:connect...
    赋闲阅读 437评论 0 0
  • 早上来到市场,对面店铺没有开门,隔壁邻居,家政开门了,胡同里别家没有开店门。 市场的客流量还是像往常一样的少。 开...
    果然越来越好阅读 204评论 0 0
  • 第二章开始讲到创业的问题。作者提出了一个很新颖的观点:创业不是一个从0到1的过程,而是一个从0到N的过程。而这个过...
    李敏佳阅读 106评论 0 0