This article will give you an in-depth talk about the use of ref, toRef, and toRefs in the vue3 project. I hope it will be helpful to everyone!
ref function can wrap simple data types into responsive data ( Complex types are also available). Note that when manipulating values in JS, you need to add the .value attribute. You can use it normally in the template.
For example:
{{ name }}
Visible writing and reactive() The same, but you need to add an additional .value when writing in js. [Related recommendations:vuejs video tutorial,web front-end development]
{{ data?.name }}
The role of toRef function: Convert a property in the responsive object to a separate responsive data, and The converted value is related to the previous one (the ref function can also be converted, but the value is not related).
Let’s look at the following example first:
name: {{ obj.name }} age: {{obj.age}}
Writing this way can also change the data into responsive data, but it brings two problems:
Question 1: obj. must be used in the template to obtain data, which is troublesome.
Problem 2: Although only name and age are used in the template, the entire obj is exported. This is unnecessary and a waste of performance.
name: {{ name }}
In this way, we can put the data we need into the return, which saves performance and writing in the template, and is a bit more like 'import on demand'
The role of toRefs function: convert all properties in the responsive object into separate responsive data, and the converted value is related to the previous one .
{{ name }} {{ age }}
toRefs will return all our responsive data. Then just use the data name directly. Remember to add...oh
according to. Without further ado, let’s learn how to use these three methods and the differences with reactive.
(Learning video sharing:vuejs introductory tutorial,Basic programming video)
The above is the detailed content of An article to talk about ref, toRef, toRefs in vue3. For more information, please follow other related articles on the PHP Chinese website!