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

に公開2

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>

https://github.com/vuejs/core/pull/7986

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

NatsukiNatsuki

3.5以降から有効になっているようです!
https://ja.vuejs.org/api/sfc-script-setup#reactive-props-destructure

okadaokada

Vue3.5以上のwithDefaultsを調べていてたどり着きました!
情報ありがとうございます!

その後、withDefaultsの非推奨はいったんなくなったみたいですね。

withDefaults() was marked as deprecated in this PR but the deprecation has been reverted in 4af5d1b.
https://github.com/vuejs/core/pull/7986

ただ、Vue3.5で分割代入と合わせて使うとおかしな動きをしていました。
分割代入とwithDefaultsの併用はできないのかもしれません。

documentでも、Vue3.5以上で分割代入を使う場合は、withDefaultsと併用する書き方はされていなかったです。
https://vuejs.org/guide/typescript/composition-api#props-default-values