iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🙆
Reactive defineProps Destructuring and withDefaults Deprecation in Vue 3
Making defineProps destructuring reactive & deprecating withDefaults — a PR for this has been merged!
Vue 3.3 looks like it will support this!
<script setup lang="ts">
const { foo = 1, bar = 'ok' } = defineProps<{ foo?: number, bar?: string }>()
</script>
Until now, there have been issues where destructuring defineProps leads to a loss of reactivity, or the declaration of default values becomes redundant when using type declarations. This seems like it will be resolved!
export interface Props {
msg?: string
labels?: string[]
}
const props = withDefaults(defineProps<Props>(), {
msg: 'hello',
labels: () => ['one', 'two']
})
Discussion
3.5以降から有効になっているようです!
Vue3.5以上のwithDefaultsを調べていてたどり着きました!
情報ありがとうございます!
その後、withDefaultsの非推奨はいったんなくなったみたいですね。
ただ、Vue3.5で分割代入と合わせて使うとおかしな動きをしていました。
分割代入とwithDefaultsの併用はできないのかもしれません。
documentでも、Vue3.5以上で分割代入を使う場合は、withDefaultsと併用する書き方はされていなかったです。