How to update the view synchronously after data changes in Vue

How to update the view synchronously after data changes in Vue

Preface

Not long ago, I saw an interesting problem. How to update the view synchronously after the data in Vue changes? I searched for it, but didn’t find a solution to the problem. I could only find a solution from the source code.

reason

We all know that after changing the data in Vue, the view is not updated synchronously.

After the vue instance is initialized, data will be set to a responsive object. When we execute this.xxx = 1, the setter of this responsive object will be triggered. In the setter, an update will be triggered to notify all subscribers who have subscribed to xxx. But this trigger update is not synchronous, it will add all watchers to a queue and update the view after nextTick.

That's why vue can't update the view synchronously.

Workaround

Once you know the cause, you can always find a solution.

Since the view is updated at nextTick, a method to update the view must be executed at this time. If we manually execute this method when the data changes, we can achieve the purpose of synchronously updating the view.

After understanding the source code, we can find that the method executed is watcher.run(), so the question is, how to get this method?

If you want to quickly understand this, it is recommended to read Vue.js Technology Unveiled

Let's print this in the console

The run method can be found on the prototype of the _watcher object, so the problem is solved.

 this.xxx = 1;
 this._watcher.run()

Execute the above code and manually update the view after updating the data to achieve the synchronization effect.

A better solution

It would be troublesome to add this._watcher.run() every time you want to update the view synchronously. Therefore, I wrote a plug-in that supports synchronous update of the view after this.xxx = 1.

The principle of this plug-in is very simple. It adds an option syncData in the component options, which is similar to data. Then put it in data. When the create hook is called, this part of data is hijacked again. When the data in syncData changes, it automatically triggers _watch.run(), thereby synchronously updating the view.

Plugin address: GitHub address

postscript

To be honest, I think this plugin is useless. In theory, all the problems that this plugin can solve can be solved by $nextTick.

This is the end of this article about how to synchronize views after data changes in Vue. For more relevant Vue view synchronization update content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Solve the problem that the data is modified in Vue but the view cannot be updated
  • Solve the problem that the view does not update when the object property changes in Vue
  • Detailed explanation of the situation where the vue view does not update

<<:  Centos7 installation of Nginx integrated Lua sample code

>>:  Solution to the problem "Table mysql.plugin doesn't exist" when deploying MySQL

Recommend

mysql IS NULL using index case explanation

Introduction The use of is null, is not null, and...

Introduction to the use of MySQL pt-slave-restart tool

Table of contents When setting up a MySQL master-...

How to change the dot in the WeChat applet swiper-dot into a slider

Table of contents background Target Effect Ideas ...

Detailed explanation of the solution to permission denied in Linux

Permission denied: The reason for this is: there ...

Six methods for nginx optimization

1. Optimize Nginx concurrency [root@proxy ~]# ab ...

Detailed explanation of the installation process of Jenkins on CentOS 7

Install Jenkins via Yum 1. Installation # yum sou...

A detailed introduction to Linux file permissions

The excellence of Linux lies in its multi-user, m...

How to implement real-time polygon refraction with threejs

Table of contents Preface Step 1: Setup and front...

Understand all aspects of HTTP Headers with pictures and text

What are HTTP Headers HTTP is an abbreviation of ...

Implementation of the Pycharm installation tutorial on Ubuntu 18.04

Method 1: Download Pycharm and install Download a...

What are the advantages of MySQL MGR?

MGR (MySQL Group Replication) is a new feature ad...

Linux kernel device driver system call notes

/**************************** * System call******...