Let's write a simple new Vue({ el: '#app', data() { return { a: 1, b: 2, c: 3, d: 4, }; }, created() { console.log(this.b); this.b = 'aaa'; }, template: '<div>Hello World{{a}}{{b}}</div>', }); In Vueinstance/state.js, proxy will be used to proxy each attribute const keys = Object.keys(data) const props = vm.$options.props const methods = vm.$options.methods let i = keys.length while (i--) { const key = keys[i] if (props && hasOwn(props, key)) { process.env.NODE_ENV !== 'production' && warn( `The data property "${key}" is already declared as a prop. ` + `Use prop default value instead.`, vm ) } else if (!isReserved(key)) { //Properties of the proxy object proxy(vm, `_data`, key) } } // observe data observe(data, true /* asRootData */) Use defineReactive to hijack each attribute in data observe(data, true /* asRootData */); // observe const keys = Object.keys(obj); for (let i = 0; i < keys.length; i++) { defineReactive(obj, keys[i]); } // defineReactive Object.defineProperty(obj, key, { enumerable: true, configurable: true, get: function reactiveGetter() { const value = getter ? getter.call(obj) : val; // The key point is here. If the attributes are used in the template later, the reactiveGetter function will be executed // Collected by the Dep class if (Dep.target) { console.log(`${key} property is collected by Dep class`) dep.depend(); if (childOb) { childOb.dep.depend(); if (Array.isArray(value)) { dependArray(value); } } } return value; }, set: function reactiveSetter(newVal) { const value = getter ? getter.call(obj) : val; /* eslint-disable no-self-compare */ if (newVal === value || (newVal !== newVal && value !== value)) { return; } if (setter) { // Here is the computed set function setter.call(obj, newVal); } else { val = newVal; } childOb = !shallow && observe(newVal); // If we change the property, we will call notify to asynchronously update the view dep.notify(); }, }); Execute $mount to mount the view if (vm.$options.el) { vm.$mount(vm.$options.el); } $mount is a method that calls the Vue prototype. The key point is the last sentence mount.call(this, el, hydrating) Vue.prototype.$mount = function ( el?: string | Element, hydrating?: boolean ): Component { el = el && query(el); const options = this.$options; // resolve template/el and convert to render function /** * Check if the render function exists? If it does not exist, parse the template template* When Vue renders a page, there are two ways: 1. template, 2. render. Ultimately, all template classes need to be rendered using render*/ if (!options.render) { let template = options.template; if (template) { if (typeof template === 'string') { if (template.charAt(0) === '#') { template = idToTemplate(template); /* istanbul ignore if */ if (process.env.NODE_ENV !== 'production' && !template) { warn( `Template element not found or is empty: ${options.template}`, this ); } } } else if (template.nodeType) { template = template.innerHTML; } else { if (process.env.NODE_ENV !== 'production') { warn('invalid template option:' + template, this); } return this; } } else if (el) { // If the template does not exist, create a default html template template = getOuterHTML(el); } } // Rewrite Vue.prototype.$mount, and finally call the cached mount method to complete the mounting of $mount return mount.call(this, el, hydrating); }; Here mount calls the mountComponent(this, el, hydrating) method, and mountComponent executes the _render function, and finally _render calls render to generate a vnode. const { render, _parentVnode } = vm.$options; vnode = render.call(vm._renderProxy, vm.$createElement); The last picture shows that If there are any mistakes in the article, please point them out and I will continue to improve them. Thank you. If you need to debug the source code, click here and follow the readme. Hope star This is the end of this article about changing a property in Vue data at will update the view? For more relevant Vue data content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Import CSS files using judgment conditions
>>: HTML+CSS3 code to realize the animation effect of the solar system planets
Table of contents 1. Install JDK Manual Installat...
MySQL 5.7.27 detailed download, installation and ...
I am writing a small program recently. Because th...
Linux virtual machine: VMware + Ubuntu 16.04.4 Wi...
Table of contents Preface know Practice makes per...
After installing docker, there will usually be a ...
We all know that the performance of applications ...
Table of contents 1. Introduction to NFS 2. NFS C...
Every website usually encounters many non-search ...
Many friends have always wanted to know how to ru...
Table of contents 1. Array deconstruction 2. Obje...
In web projects we often use the timeline control...
Table of contents Impact of full table scan on th...
This article will explain the composition of the ...
Copy code The code is as follows: 1. Sina Weibo &...