toRefs
toRefs
オブジェクトの ref も受け入れる拡張された toRefs。
使用法
import { toRefs } from '@vueuse/core'
import { reactive, ref } from 'vue'
const objRef = ref({ a: 'a', b: 0 })
const arrRef = ref(['a', 0])
const { a, b } = toRefs(objRef)
const [a, b] = toRefs(arrRef)
const obj = reactive({ a: 'a', b: 0 })
const arr = reactive(['a', 0])
const { a, b } = toRefs(obj)
const [a, b] = toRefs(arr)
ユースケース
props オブジェクトの分割代入
<script lang="ts">
import { toRefs, useVModel } from '@vueuse/core'
export default {
setup(props) {
const refs = toRefs(useVModel(props, 'data'))
console.log(refs.a.value) // props.data.a
refs.a.value = 'a' // emit('update:data', { ...props.data, a: 'a' })
return { ...refs }
}
}
</script>
<template>
<div>
<input v-model="a" type="text">
<input v-model="b" type="text">
</div>
</template>
© 2019–PRESENT Anthony Fu https://github.com/antfu
※このページは Nuxt.js 公式ドキュメントの翻訳ページです。
公式ドキュメントの該当ページはこちら:
#