Learn and understand vue source code together
According to the official website of vue, we can know that vue is an mvvm framework and is responsive. In order to have a deeper understanding of its connotation, I implemented a simple mvvm learning demo. Share it with everyone below and welcome everyone to discuss it together.
1. At least the content of mvvm
Instruction set, such as: text, model, etc.
Data model, and View interactive data
Component support: that is, dynamic update of part of the html code
2. My implementation
1. Definition of variables and implementation of watch
var directives = {}; //指令集合var vNodes = new Array(); //解析的Dom集合var dataModel = { name:"name", title: "title"}; //数据Modelvar Watch = { isInit: false, watchs: new Array(), run: function(newValue, expOrfn){ var self = this;if(!self.isInit){ expOrfn.call(vModel); }this.watchs.map(function(data,index){ data.nodes.map(function(d,i){if(self.isInit){ d.directive.init(newValue, d, data); //绑定初始化值, 以及初始化一些事件}else{ d.directive.update(newValue, d, data); //只更新值,此时run的调用来值value-set } }); }); self.watchs = []; }, push:function(watch){this.watchs.push(watch); } } //任务管理
Description:
Watch push method , used to add dependencies, and then run to execute all dependencies. After the execution is completed, the current dependency collection needs to be cleaned up. The collection of dependencies in vue is completed in dep, and the task management provided by watch (I don’t know if I understand it correctly)
2. Definition of instructions
directives.text = { init: function(value, vNode){ vNode.elm.textContent = value; }, update: function(value, vNode){ vNode.elm.textContent = value; } }//需要响应事件的怎么办directives.model ={ init: function(value, vNode, _watch){ vNode.elm.value = value; //判断自己发生的改变,不应该再改变自己 vNode.elm.addEventListener('keyup',function(evt){ vNode.model[_watch.key] = vNode.elm.value; }); }, update:function(){ } }
Description:
Since it is a demo learning example, only two simple text and model specifications are defined. , text: used for data display, and model used for input (input box) response
3. Generation of vModel
//转换vModel,暂支持一级var properties = Object.getOwnPropertyNames(dataModel);var vModel = {}, formSetting = false;for( var index in properties){ (function refreshData(_index){var key = properties[_index];var property = Object.getOwnPropertyDescriptor(dataModel, key);var setter = property.set;var getter = property.get;var _val = property.value;var _getter = function(){var val = getter ? getter.call(vModel) : _val;//收集依赖,与watch要分开 Watch.push({ key: key, nodes: vNodes.filter(function(data,index){return data.modelKey == key ? true : false; }), getter: _getter });return val; }; Object.defineProperty(vModel, key, { configurable: true, enumerable: true, set: function(value){if(setter){ setter.call(vModel, value); } //处理依赖 Watch.run(value, _getter);//this.value = value; }, get: _getter }) })(index); }
Note:
vModel is generated based on dataModel, that is, the get and set methods of each attribute are customized. It can also be implemented using proxy in es6 ( Is it correct? ).
When setting an attribute, the get method will be called first to collect dependencies. After the convenience value is changed, all the affected content can be modified.
4. Parse dom as vNode
//解析vNodesvar app = document.getElementById('app'); app.childNodes.forEach(function(data,index){if(data.nodeType != 1) return;var hv = data.getAttribute('data-hv');var hvs = hv.split(','); hvs.forEach(function(item,row){var keyValue = item.split(':'); //vNode对象上一定要有model,这是方便vNode相应时候的找vModel vNodes.push({ directive: directives[keyValue[0]], modelKey: keyValue[1], model: vModel, elm: data }); }); });
Instructions:
Here it is parsed as vNode It is very far-fetched, because this only collects the instructions specified by data-hv on the dom, and stores the corresponding instructions, elements, vModel, etc. into an object in vNodes for reference when the get method of each attribute of the vModel collects dependencies.
5. First initialization
//调用所有的get一次Watch.isInit = true;var _keys = Object.getOwnPropertyNames(vModel); _keys.map(function(key,data){var data = vModel[key]; Watch.run(data); }); Watch.isInit = false;
Instructions:
The vModel that will be initialized The value is rendered to the Dom. Here, each get is actively executed, and then the watch.run method is run.
I feel that the design and implementation here are not consistent with vue’s ideas. If anyone sees this, please give me some advice and guidance.
6. Parsed dom
<div id="app"><span data-hv="text:title"></span><span data-hv="text:title"></span><input data-hv="model:title" /></div>
The above is the detailed content of Learn and understand vue source code together. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



As a high-level programming language, Python language is easy to learn, easy to read and write, and has been widely used in the field of software development. However, due to the open source nature of Python, the source code is easily accessible to others, which brings some challenges to software source code protection. Therefore, in practical applications, we often need to take some methods to protect Python source code and ensure its security. In software source code protection, there are a variety of application practices for Python to choose from. Below are some common

How to display the source code of PHP code in the browser without being interpreted and executed? PHP is a server-side scripting language commonly used to develop dynamic web pages. When a PHP file is requested on the server, the server interprets and executes the PHP code in it and sends the final HTML content to the browser for display. However, sometimes we want to display the source code of the PHP file directly in the browser instead of being executed. This article will introduce how to display the source code of PHP code in the browser without being interpreted and executed. In PHP, you can use

You can use the browser's developer tools to view the source code of the website. In the Google Chrome browser: 1. Open the Chrome browser and visit the website where you want to view the source code; 2. Right-click anywhere on the web page and select "Inspect" or press the shortcut key Ctrl + Shift + I to open the developer tools; 3. In the top menu bar of the developer tools, select the "Elements" tab; 4. Just see the HTML and CSS code of the website.

By understanding the Golang framework source code, developers can master the essence of the language and expand the framework's functions. First, get the source code and become familiar with its directory structure. Second, read the code, trace the execution flow, and understand dependencies. Practical examples show how to apply this knowledge: create custom middleware and extend the routing system. Best practices include learning step-by-step, avoiding mindless copy-pasting, utilizing tools, and referring to online resources.

Vue can display the source code. The method for viewing the source code in Vue is: 1. Obtain Vue through "git clone https://github.com/vuejs/vue.git"; 2. Install dependencies through "npm i"; 3. Through " npm i -g rollup" to install rollup; 4. Modify the dev script; 5. Debug the source code.

Steps to view tomcat source code in IDEA: 1. Download Tomcat source code; 2. Import Tomcat source code in IDEA; 3. View Tomcat source code; 4. Understand the working principle of Tomcat; 5. Precautions; 6. Continuous learning and updating ; 7. Use tools and plug-ins; 8. Participate in the community and contribute. Detailed introduction: 1. Download the Tomcat source code. You can download the source code package from the official website of Apache Tomcat. Usually these source code packages are in ZIP or TAR format, etc.

This article explores the source code analysis and optimization of the Go framework. The source code structure includes four main packages, involving core framework logic, request context, data binding and response rendering. Optimization techniques include: 1. Use routing trees to optimize route processing to significantly increase route lookup speed. 2. Use middleware for caching and compression to reduce server load and response time. 3. Avoid performing time-consuming operations in callbacks to maintain high responsiveness. 4. Enable logging and analyze slow requests to identify performance bottlenecks. 5. Update framework versions regularly to take advantage of the latest performance improvements.

PHP source code error: To solve the index error problem, specific code examples are needed. With the rapid development of the Internet, developers often encounter various problems when writing websites and applications. Among them, PHP is a popular server-side scripting language, and its source code errors are one of the problems that developers often encounter. Sometimes, when we try to open the index page of a website, various error messages will appear, such as "InternalServerError", "Unde
