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

WeChat applet realizes simple tab switching effect

This article shares the specific code for WeChat ...

impress.js presentation layer framework (demonstration tool) - first experience

I haven’t blogged for half a year, which I feel a ...

Idea configures tomcat to start a web project graphic tutorial

Configure tomcat 1. Click run configuration 2. Se...

Detailed explanation of Vue filter implementation and application scenarios

1. Brief Introduction Vue.js allows you to define...

Solution to JS out-of-precision number problem

The most understandable explanation of the accura...

How to use Docker to build enterprise-level custom images

Preface Before leaving get off work, the author r...

Summary of pitfalls in importing ova files into vmware

Source of the problem As we all know, all network...

Two query methods when the MySQL query field type is json

The table structure is as follows: id varchar(32)...

Example of implementing dashed border with html2canvas

html2canvas is a library that generates canvas fr...

Semantics: Is Html/Xhtml really standards-compliant?

<br />Original text: http://jorux.com/archiv...

Tips for optimizing MySQL SQL statements

When faced with a SQL statement that is not optim...

HTML meta usage examples

Example Usage Copy code The code is as follows: &l...

Writing Snake Game with Native JS

This article shares the specific code of writing ...