-
toRef函数是创建一个引用实现对象(ObjectRefImpl),并将它指向另一个对象中的某个属性
<template> <p>{{name}}</p> <p>{{city}}</p> <button @click="name + '~'">更改姓名</button> <!-- 更改name实际上更改了person.name --> <button @click="city + '~'">更改城市</button> </template>
<script> import { reactive, toRef } from 'vue'; export default { setup() { let person = reactive({ name: 'wu', job: { city: 'tj' } }); return { name: toRef(person, name), city: toRef(person.job, city) }; } } </script>
-
toRefs是批量操作
<template> <p>{{name}}</p> <p>{{job.city}}</p> <button @click="name + '~'">更改姓名</button> <!-- 更改name实际上更改了person.name --> <button @click="job.city + '~'">更改城市</button> </template>
<script> import { reactive, toRefs } from 'vue'; export default { setup() { let person = reactive({ name: 'wu', job: { city: 'tj' } }); return { ...toRefs(person) }; } } </script>
Vue3的toRef和toRefs
最新推荐文章于 2023-11-28 19:05:06 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)