本文主要是介绍Vue3-toRef 和 toRefs 函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Vue3-toRef 和 toRefs 函数
- 功能:可以简化语法调用。
- toRef
- 函数执行时会生成一个对象 ObjectRefImpl ,是一个引用对象,具有value属性(getter 和 setter 属性)
- 语法格式:
toRef(对象名, '对象中的属性名')
- toRefs 语法格式:
toRefs(对象名)
// App.vue
<template><h2>计数器1:{{counter1}}</h2><button @click="counter1++">计数器1加1</button><hr>// 如果使用 toRef 那么 a. 就可以不写<h2>计数器2:{{a.counter2}}</h2><button @click="a.counter2++">计数器2加1</button>
</template><script>import { reactive, toRef, toRefs } from 'vue'export default {name : 'App',setup(){let data = reactive({counter1 : 1,a : {counter2 : 100}})// toRefreturn {counter1 : toRef(data, 'counter1'),counter2 : toRef(data.a, 'counter2')}// toRefs// 使用 toRefs 只能将调用时相同的语法进行简化return {...toRefs(data)}}}
</script>
这篇关于Vue3-toRef 和 toRefs 函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!