Home >Web Front-end >Vue.js >What are the three elements of the vue directive?
The three elements of the vue directive are responsiveness, template engine and rendering. Responsiveness means that when data is updated or added, the page will respond and the corresponding data will be re-rendered; the template engine is essentially a string, used as the identifier of the instance; rendering refers to the process of converting the template into other codes.
The operating environment of this article: Windows 10 system, Vue version 2.9.6, DELL G3 computer.
The three elements in veu
How vue is implemented as responsive
Object.definePropertySimulation
1) What is responsiveness
After modifying the data attribute, vue immediately listensthe data attribute is proxied Go to vm2) Object.defineProperty Syntax:Object.defineProperty(obj, prop, descriptor)Parameter description:
var obj = { name: 'zhangsan', age: 25 } console.log(obj.name); // 获取属性的时候,如何监听 obj.age = 26; // 赋值属性的时候,如何监听We use the defineProperty method to implement the above operation: as follows
var obj = {} var name = 'zhangsan' Object.defineProperty(obj, "name", { get: function () { console.log('get'); return name; }, set: function (newVal) { console.log('set'); name = newVal; } }); console.log(obj.name); // 可以监听到 obj.name = 'lisi'; // 可以监听到Using defineProperty we can monitor The data has changed. This is also the core method for vue to do response work.
3) Simulation
var mv = {} var data = { price: 100, name: 'zhangsan' } var key, value; for (key in data) { // 命中闭包。新建一个函数,保证 key 的独立的作用域 (function (key) { Object.defineProperty(mv, key, { get: function () { console.log('get'); return data[key]; }, set: function (newVal) { console.log('set'); data[key] = newVal } }) })(key); }How the vue template is parsedWhat is the templaterender functionrender function and vdom
1) What is a template
Essence: stringhas logic, such as v-if v-for, etc.It is very similar to html format, but there is a big differenceFinally it has to be converted into html for displayThe template must eventually be converted into JS code, because:There is logic (v-if v-for), which must be realized with JS (Turing complete)Convert to html rendering page, which must be realized with JSTherefore, the template The most important thing is to convert it into a JS functionBasic example
<div id="app"> <div> <input v-model="title"> <button v-on:click="add">submit</button> </div> <ul> <li v-for="item in list">{{item}}</li> </ul> </div>The above is a template. [Related recommendations: "
vue.js Tutorial"]
The above is the detailed content of What are the three elements of the vue directive?. For more information, please follow other related articles on the PHP Chinese website!