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:
<template> <div> <div>{{ name }}</div> <button>修改数据</button> </div></template><script> import { ref } from 'vue' export default { name: 'App', setup() { const name = ref('初映') const updateName = () => { name.value = '初映CY的前说' } return { name, updateName } }, }</script>
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]
<template> <div> <div>{{ data?.name }}</div> <button>修改数据</button> </div></template><script> import { ref } from 'vue' export default { name: 'App', setup() { // 初始值是 null const data = ref(null) setTimeout(() => { // 右边的对象可能是后端返回的 data.value = { name: '初映', } }, 1000) const updateName = () => { data.value.name = 'CY' } return { data, updateName } }, }</script>
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:
<template> <div> <h2>name: {{ obj.name }} age: {{obj.age}}</h2> <button>修改数据</button> </div></template><script> import { reactive } from 'vue' export default { name: 'App', setup() { const obj = reactive({ name: '初映', age: 18, address: '江西', sex: '男', }) const updateName = () => { obj.name = '初映CY的前说' } return { obj, updateName } }, }</script>
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.
<template> <div> <h2>name: {{ name }} </h2> <button>修改数据</button> </div></template><script> import { reactive,toRef } from 'vue' export default { name: 'App', setup() { const obj = reactive({ name: '初映', age: 18, address: '江西', sex: '男', }) const name = toRef(obj, 'name') const updateName = () => { obj.name = '初映CY的前说' } return { name, updateName } }, }</script>
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 .
<template> <div> <h2>{{ name }} {{ age }}</h2> <button>修改数据</button> </div></template><script> import { reactive, toRefs } from 'vue' export default { name: 'App', setup() { const obj = reactive({ name: '初映', age: 10, }) const updateName = () => { obj.name = '初映CY的前说' obj.age = 18 } return { ...toRefs(obj), updateName } }, }</script>
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!