How to understand Refs and Ref in Vue3
This article shows you how to understand Refs and Ref in Vue3. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
The editor shares with you the corresponding questions about the data in Vue3. Let's give an example of this.
Vue.createApp ({template: `{nameObj.name}}`, setup () {const {reactive} = Vue const nameObj = reactive ({name:'lilei',age:18}) setTimeout (() = > {nameObj.name = 'hanmeimei'}, 2000) return {nameObj}) .mount (' # root')
At this time, we may think of the content about deconstruction assignment in es6. Can we get the internal name by deconstructing the nameObj in the above example, and then return the name directly? That is, to write the code like this.
Vue.createApp ({template: `{name}}`, setup () {const {reactive,toRefs} = Vue const nameObj = reactive ({name:'lilei',age:18}) let {name} = nameObj setTimeout (() = > {name.value = 'hanmeimei'}, 2000) return {name}}) .mount (' # root')
In the actual operation, we found that the content on the page did not become hanmeimei. At this time, we need to introduce another content in Vue3. We should modify the code like this in order to achieve the responsiveness of data and pages.
Vue.createApp ({template: `{name}}`, setup () {const {reactive,toRefs} = Vue const nameObj = reactive ({name:'lilei',age:18}) let {name} = toRefs (nameObj) setTimeout (() = > {name.value = 'hanmeimei'}, 2000) return {name}}) .mount (' # root')
Again, very similar to toRefs is toRef, where the code example looks like this
Vue.createApp ({template: `{age}}`, setup () {const {reactive,toRef} = Vue const nameObj = reactive ({name:'lilei'}) let age = toRef (nameObj,'age') setTimeout (() = > {age.value = 'hanmeimei'}) 2000) return {age}}) .mount ('# root') the above is how to understand Refs and Ref in Vue3 Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.