search
HomeWeb Front-endJS TutorialArray change detection problem in vue

Array change detection problem in vue

Apr 17, 2018 pm 03:36 PM
VarietyDetectionquestion

This time I will bring you the problem of array change detection in vue. What are the precautions for array change detection in vue? The following is a practical case, let's take a look.

nbsp;html>


 <meta>
 <title>vue</title>
 <script></script>
 <style>
  li:hover {
   cursor: pointer;
  }
 </style>


 <p>
  </p>
       
  •     {{item.name}}     {{numbers[index]}}    
  •   
   <script> var vm = new Vue({ el: ".wrap", data: { numbers: [], items: [ {name: &#39;jjj&#39;}, {name: &#39;kkk&#39;}, {name: &#39;lll&#39;}, ] }, methods: { handle: function (index) { // WHY: 更新数据,view层未渲染,但通过console这个数组可以发现数据确实更新了 if (typeof(this.numbers[index]) === "undefined" ) { this.numbers[index] = 1; } else { this.numbers[index]++; } } } }); </script> The implementation purpose here is very clear --- I hope to detect whether li exists when clicking it. Of course it does not exist, so I set the value to 1. If I click it again, let the number accumulate.

But the problem that arises is: after clicking, the number is not updated in the view layer, and through console printing, it is found that the data is indeed updated, but the view layer does not detect it in time. And what I have been thinking is: Since Vue implements two-way data binding, why is it not updated in the view layer after the model layer changes?

First, I considered whether this was an array problem, so I tested the following example:

Example 2

nbsp;html>


 <meta>
 <title>vue</title>
 <script></script>
 <style>
  li:hover {
   cursor: pointer;
  }
 </style>


 <p>
  </p>
       
  •     {{item.name}}     {{numbers[index]}}    
  •   
   <script> var vm = new Vue({ el: ".wrap", data: { numbers: [], items: [ {name: &#39;jjj&#39;}, {name: &#39;kkk&#39;}, {name: &#39;lll&#39;}, ] }, methods: { handle: function (index) { // 不是数组,这里更新数据就可以直接在view层渲染 this.items[index].name += " success"; } } }); </script> At this time, when I tested again, I found that when the model layer here changes, the view layer can be updated in a timely and effective manner.

The most important sentence is---if the object is responsive, ensure that the

property is also responsive after it is created, and triggers a view update. This method is mainly used to avoid Vue from being able to detect the property. restrictions were added.

So under what circumstances can Vue fail to detect that attributes have been added? According to the reference link, we saw a good explanation in the document --- In-depth reactive principle

First, we need to understand how Vue implements two-way binding of data!

Pass a normal

JavaScript object to the data option of the Vue instance. Vue will traverse all the properties of this object and use Object.defineProperty Converts all these properties to getters/setters. Object.defineProperty is only ES5 supports but cannot shim features, which is why Vue does not support IE8 and lower browsers.

Knowledge supplement:

Accessor properties do not contain data values; they contain a pair of getter and setter functions (these two functions are not required). When the accessor property is read, the getter function is called, which is responsible for returning a valid value; when the accessor property is written, the setter function is called and the new value is passed in. This function is responsible for deciding how to process the data.

Accessor properties cannot be defined directly and must be defined using Object.defineProperty().

Here is an example:

nbsp;html>


 <meta>
 <title>vue</title>


 <script>
  var book={
    _year:2004,
    edition:1
  };
  Object.defineProperty(book,"year",{
    get:function(){
      return this._year;
    },
    set:function(newValue){
      if(newValue>2004){
        this._year=newValue;
        this.edition+=newValue-2004;
      }
    }
  });
  console.log(book.year); // 2004 在读取访问器属性时会调用get函数
  book.year=2005; // 在给访问器属性赋值时会调用set函数
  console.log(book.edition); // 2
 </script>

This example should provide a good understanding of accessor properties.

Therefore, when the accessor attribute value under the object changes (Vue will convert the attributes into accessor attributes, as mentioned before), then the set function will be called, and then Vue can track the changes through this set function. , call related functions to update the view.

Each component instance has a corresponding watcher instance object, which records the properties as dependencies during component rendering. Later, when the dependency's setter is called, the watcher will be notified to recalculate, causing its associated components to be updated. . That is, during the rendering process, the getter function of the object attribute will be called, and then the getter function will notify the water object to declare it as a dependency. After the dependency, if the object attribute changes, the setter function will be called to notify the watcher, and the watcher will Re-render the component to complete the update.

OK! Now that we know the principle, we can further understand why the previous array problem occurred!

Change detection issues

Received by the limitations of modern JavaScript browsers, it is actually mainly Object.observe() The method support is not good, and Vue cannot detect the addition or deletion of objects. However, Vue performs the setter/getter conversion process on the property when initializing the instance, so the property must be on the object from the beginning in order for Vue to convert it.

所以对于前面的例子就不能理解了 --- 数组中index都可以看做是属性,当我们添加属性并赋值时,Vue并不能检测到对象中属性的添加或者删除,但是其的确是添加或删除了,故我们可以通过console看到变化,所以就没有办法做到响应式; 而在第二个例子中,我们是在已有的属性的基础上进行修改的,这些属性是在最开始就被Vue初始化实例时执行了setter/getter的转化过程,所以说他们的修改是有效的,model的数据可以实时的在view层中得到相应。

补充知识: 什么是 Object.observe() ?

在介绍之前,不得不残忍的说,尽管这个方法可以在某些浏览器上运行,但事实是这个方法已经废弃!

概述: 此方法用于异步地监视一个对象的修改。当对象的属性被修改时,方法的回调函数会提供一个有序的修改流,然而这个接口已经从各大浏览器移除,可以使用通用的proxy 对象。      

方法:

Object.observe(obj, callback[, acceptList])

其中obj就是被监控的对象, callback是一个回调函数,其中的参数包括changes和acceptList,

changes一个数组,其中包含的每一个对象代表一个修改行为。每个修改行为的对象包含:

  • name: 被修改的属性名称。

  • object: 修改后该对象的值。

  • type: 表示对该对象做了何种类型的修改,可能的值为"add", "update", or "delete"。

  • oldValue: 对象修改前的值。该值只在"update"与"delete"有效。

acceptList在给定对象上给定回调中要监视的变化类型列表。如果省略, ["add", "update", "delete", "reconfigure", "setPrototype", "preventExtensions"] 将会被使用。

var obj = {
 foo: 0,
 bar: 1
};
Object.observe(obj, function(changes) {
 console.log(changes);
});
obj.baz = 2;
// [{name: 'baz', object: <obj>, type: 'add'}]
obj.foo = 'hello';
// [{name: 'foo', object: <obj>, type: 'update', oldValue: 0}]
delete obj.baz;
// [{name: 'baz', object: <obj>, type: 'delete', oldValue: 2}]</obj></obj></obj>

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

layer前端组件图片显示功能

web前端必看4本开发书籍

The above is the detailed content of Array change detection problem in vue. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools