The following Vue.js tutorial column will take you to understand the data initialization (initState) in vue. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Data initialization
The Vue instance will run a series of initialization operations when it is created. Among these initialization operations, the one most closely associated with data binding is initState.
First of all, let’s take a look at his code:
function initState(vm) { vm._watchers = []; var opts = vm.$options; if(opts.props) { initProps(vm, opts.props); //初始化props } if(opts.methods) { initMethods(vm, opts.methods); //初始化methods } if(opts.data) { initData(vm); //初始化data } else { observe(vm._data = {}, true /* asRootData */ ); } if(opts.computed) { initComputed(vm, opts.computed); //初始化computed } if(opts.watch && opts.watch !== nativeWatch) { initWatch(vm, opts.watch); //初始化watch } }
In the initialization of so much data, props, methods and data are relatively simple (so I won’t introduce them in detail☺) , while computed and watch are relatively difficult and have complicated logic, so I will mainly talk about computed and watch below (the following code part is simplified).
InitState mainly initializes the props, methods, data, computed and watch data in the vue instance.
When initializing props (initProps), each attribute in props will be traversed, and then type verification, data monitoring, etc. will be performed (a hook function that will throw a warning when assigning a value to props attributes) is provided.
When initializing methods (initMethods), it is mainly to monitor whether the method names in methods are legal.
When initializing data (initData), the observe function will be run to deeply traverse each attribute in the data to perform data hijacking.
When initializing computed (initComputed), it will monitor whether the data already exists in data or props. If it exists, a warning will be thrown. Otherwise, the defineComputed function will be called, monitor the data, and bind getters and properties to the properties in the component. setter. If the value of an attribute in computed is a function, it defaults to the attribute's getter function. In addition, the value of the attribute can also be an object. It has only three valid fields: set, get and cache, which respectively indicate the attribute's setter, getter and whether to enable caching. Get is required and cache defaults to true.
function initComputed(vm, computed) { var watchers = vm._computedWatchers = Object.create(null); for(var key in computed) { var userDef = computed[key]; var getter = typeof userDef === 'function' ? userDef : userDef.get; //创建一个计算属性 watcher watchers[key] = new Watcher( vm, getter || noop, noop, computedWatcherOptions ); if(!(key in vm)) { //如果定义的计算属性不在组件实例上,对属性进行数据劫持 //defineComputed 很重要,下面我们再说 defineComputed(vm, key, userDef); } else { //如果定义的计算属性在data和props有,抛出警告 } } }
When initializing watch (initWatch), the vm.$watch function will be called to bind setter callbacks to the properties in the watch (if there is no such property in the component, it cannot be successfully monitored. The property must exist in props, data or computed). If the value of the attribute in the watch is a function, the default is the setter callback function of the attribute. If the value of the attribute is an array, the contents of the array are traversed and callbacks are bound to the attributes respectively. In addition, the value of the attribute can also be an object. , at this time, the handler field in the object represents the setter callback function, immediate represents whether to execute the handler method inside immediately, and deep represents whether to monitor deeply.
vm.$watch function will directly use Watcher to construct the observer object. The value of the attribute in watch exists as watcher.cb, and is executed in the watcher.run function when the observer updates. If you want to understand this process, you can read my introduction to Watcher in the vue responsive system-observe, watcher, and dep in my previous article.
function initWatch(vm, watch) { //遍历watch,为每一个属性创建侦听器 for(var key in watch) { var handler = watch[key]; //如果属性值是一个数组,则遍历数组,为属性创建多个侦听器 //createWatcher函数中封装了vm.$watch,会在vm.$watch中创建侦听器 if(Array.isArray(handler)) { for(var i = 0; i < handler.length; i++) { createWatcher(vm, key, handler[i]); } } else { //为属性创建侦听器 createWatcher(vm, key, handler); } } } function createWatcher(vm, expOrFn, handler, options) { //如果属性值是一个对象,则取对象的handler属性作为回调 if(isPlainObject(handler)) { options = handler; handler = handler.handler; } //如果属性值是一个字符串,则从组件实例上寻找 if(typeof handler === 'string') { handler = vm[handler]; } //为属性创建侦听器 return vm.$watch(expOrFn, handler, options) }
computed
computed is essentially a lazy-evaluated observer with cacheability. Only when dependencies change, the first time The new value will be calculated only after accessing the computed attribute
The following will be explained around this sentence.
As mentioned in the above code, when the data in the calculated attribute exists in data and props, you will be warned, which means that this approach is wrong. So generally, we will declare data directly in calculated properties. In the same code snippet, if the defined computed property is not on the component instance, the defineComputed function will be run to perform data hijacking on the data. Let's take a look at what is done in the defineComputed function.
function defineComputed(target, key, userDef) { //是不是服务端渲染 var shouldCache = !isServerRendering(); //如果我们把计算属性的值写成一个函数,这时函数默认为计算属性的get if(typeof userDef === 'function') { sharedPropertyDefinition.get = shouldCache ? //如果不是服务端渲染,则默认使用缓存,设置get为createComputedGetter创建的缓存函数 createComputedGetter(key) : //否则不使用缓存,直接设置get为userDef这个我们定义的函数 userDef; //设置set为空函数 sharedPropertyDefinition.set = noop; } else { //如果我们把计算属性的值写成一个对象,对象中可能包含set、get和cache三个字段 sharedPropertyDefinition.get = userDef.get ? shouldCache && userDef.cache !== false ? //如果我们传入了get字段,且不是服务端渲染,且cache不为false, //设置get为createComputedGetter创建的缓存函数 createComputedGetter(key) : //如果我们传入了get字段,但是是服务端渲染或者cache设为了false,设置get为userDef这个我们定义的函数 userDef.get : //如果没有传入get字段,设置get为空函数 noop; //设置set为我们传入的传入set字段或空函数 sharedPropertyDefinition.set = userDef.set ? userDef.set : noop; } //虽然这里可以get、set都可以设置为空函数 //但是在项目中,get为空函数对数据取值会报错,set为空函数对数据赋值会报错 //而computed主要作用就是计算取值的,所以get字段是必须的 //数据劫持 Object.defineProperty(target, key, sharedPropertyDefinition); }
In the previous article vue responsive system--observe, watcher, dep, I mentioned in the introduction about Watcher that when the calculated attribute watcher is instantiated, it will Set options.lazy to true. This is the key to lazy evaluation of calculated properties and being cacheable. Of course, the premise is that cache is not false.
If the cache is not false, the createComputedGetter function will be called to create the getter function computedGetter of the calculated attribute.
Let’s look at a piece of code first
function createComputedGetter(key) { return function computedGetter() { var watcher = this._computedWatchers && this._computedWatchers[key]; if(watcher) { if(watcher.dirty) { //watcher.evaluate中更新watcher的值,并把watcher.dirty设置为false //这样等下次依赖更新的时候才会把watcher.dirty设置为true, //然后进行取值的时候才会再次运行这个函数 watcher.evaluate(); } //依赖追踪 if(Dep.target) { watcher.depend(); } //返回watcher的值 return watcher.value } } } //对于计算属性,当取值计算属性时,发现计算属性的watcher的dirty是true //说明数据不是最新的了,需要重新计算,这里就是重新计算计算属性的值。 Watcher.prototype.evaluate = function evaluate() { this.value = this.get(); this.dirty = false; }; //当一个依赖改变的时候,通知它update Watcher.prototype.update = function update() { //三种watcher,只有计算属性 watcher的lazy设置了true,表示启用惰性求值 if(this.lazy) { this.dirty = true; } else if(this.sync) { //标记为同步计算的直接运行run,三大类型暂无,所以基本会走下面的queueWatcher this.run(); } else { //将watcher推入观察者队列中,下一个tick时调用。 //也就是数据变化不是立即就去更新的,而是异步批量去更新的 queueWatcher(this); } };
When options.lazy is set to true ( Only the options.lazy of the calculated attribute watcher is set to true). Every time a dependency is updated, the run function will not be actively triggered, but watcher.dirty will be set to true. In this way, when the calculated property is evaluated, the computedGetter function will be run. The computedGetter function has a judgment about watcher.dirty. When watcher.dirty is true, watcher.evaluate will be run to update the value and watcher. dirty is set to false, thus completing the lazy evaluation process. As long as the dependency is not updated later, update will not be run and watcher.dirty will not be set to true. Then watcher.evaluate will not be run to update the value when the value is obtained again, thereby achieving the cache effect.
In summary, we understand that when the cache is not false, the computed properties are lazy evaluated and cacheable, and the cache defaults to true, and we mostly use this default value, so we say computed Essentially, it is a lazy-evaluating observer with cacheability. Only when the dependency changes and the computed attribute is accessed for the first time, the new value will be calculated
.
Related recommendations:
2020 front-end vue interview questions summary (with answers)
vue tutorial Recommendation: The latest 5 vue.js video tutorial selections in 2020
For more programming-related knowledge, please visit: Programming Teaching! !
The above is the detailed content of Detailed explanation of data initialization (initState) in vue. For more information, please follow other related articles on the PHP Chinese website!

Vue.js is loved by developers because it is easy to use and powerful. 1) Its responsive data binding system automatically updates the view. 2) The component system improves the reusability and maintainability of the code. 3) Computing properties and listeners enhance the readability and performance of the code. 4) Using VueDevtools and checking for console errors are common debugging techniques. 5) Performance optimization includes the use of key attributes, computed attributes and keep-alive components. 6) Best practices include clear component naming, the use of single-file components and the rational use of life cycle hooks.

Vue.js is a progressive JavaScript framework suitable for building efficient and maintainable front-end applications. Its key features include: 1. Responsive data binding, 2. Component development, 3. Virtual DOM. Through these features, Vue.js simplifies the development process, improves application performance and maintainability, making it very popular in modern web development.

Vue.js and React each have their own advantages and disadvantages, and the choice depends on project requirements and team conditions. 1) Vue.js is suitable for small projects and beginners because of its simplicity and easy to use; 2) React is suitable for large projects and complex UIs because of its rich ecosystem and component design.

Vue.js improves user experience through multiple functions: 1. Responsive system realizes real-time data feedback; 2. Component development improves code reusability; 3. VueRouter provides smooth navigation; 4. Dynamic data binding and transition animation enhance interaction effect; 5. Error processing mechanism ensures user feedback; 6. Performance optimization and best practices improve application performance.

Vue.js' role in web development is to act as a progressive JavaScript framework that simplifies the development process and improves efficiency. 1) It enables developers to focus on business logic through responsive data binding and component development. 2) The working principle of Vue.js relies on responsive systems and virtual DOM to optimize performance. 3) In actual projects, it is common practice to use Vuex to manage global state and optimize data responsiveness.

Vue.js is a progressive JavaScript framework released by You Yuxi in 2014 to build a user interface. Its core advantages include: 1. Responsive data binding, automatic update view of data changes; 2. Component development, the UI can be split into independent and reusable components.

Netflix uses React as its front-end framework. 1) React's componentized development model and strong ecosystem are the main reasons why Netflix chose it. 2) Through componentization, Netflix splits complex interfaces into manageable chunks such as video players, recommendation lists and user comments. 3) React's virtual DOM and component life cycle optimizes rendering efficiency and user interaction management.

Netflix's choice in front-end technology mainly focuses on three aspects: performance optimization, scalability and user experience. 1. Performance optimization: Netflix chose React as the main framework and developed tools such as SpeedCurve and Boomerang to monitor and optimize the user experience. 2. Scalability: They adopt a micro front-end architecture, splitting applications into independent modules, improving development efficiency and system scalability. 3. User experience: Netflix uses the Material-UI component library to continuously optimize the interface through A/B testing and user feedback to ensure consistency and aesthetics.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.