1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

NuxtでvueのdefineModelを使ってみた

Last updated at Posted at 2023-06-01

実験的な新機能らしい

まず、vueのバージョンを最新のVue3.3にしておきます。

実験的な機能のためそのままでは使えないので、
使えるようにviteとかの設定を変えないといけない。

Nuxtの場合はこんな感じ

nuxt.config.ts
export default defineNuxtConfig({
  vite: {
        vue: {
            script: {
                defineModel: true,
                propsDestructure: true
            }
        }
    }
...

親のコンポーネントは普通にv-modelする。

parent.vue
<script lang="ts" setup>
	const count = ref<number>(0);
</script>

<template>
    <p>{{ count }}</p>
	<Child v-model="count" />
</template>

子供でdefineModelする。

child.vue
<script setup lang="ts">
	const count = defineModel<number>();
</script>

<template>
	<p>{{ count }}</p>
    <button @click="count!++">click</button>
</template>

  
v-modelに別名つけたい時はこうするのかな

//parent
v-model:sampleName="count"


//child
const count = defineModel<number>('sampleName');

   
あとオプションとかも色々あるみたいだけどまだよく分からない。


1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?