LoginSignup
2
2

More than 1 year has passed since last update.

Nuxt3 / Vue3 – 親コンポーネントで定義したdataを子コンポーネントの props として渡して連動させる ( Compotision API )

Last updated at Posted at 2023-03-07

どうやるんだろうと思ったがとりあえず以下で動いている
computed とか必要なかったようだ

呼び出し側

pages/example.vue

<template>
  <Example :name="name"/>
  Parent: Name :{{ name }}
  <button @click="setName()">Chage Name</button>
</template>

<script lang="ts" setup>

function setName(){
  name.value = "Bob"
}
const name = ref('Alice')

</script>

呼び出され側

componens/Example.vue

<template>
  <h3>Child: Name : {{ name }}</h3>

</template>

<script lang="ts" setup>

interface Props {
  name: string
}

withDefaults(defineProps<Props>(), {
  name: '',
})

// このように定数を定義しなくても使える
// const props = withDefaults(defineProps<Props>(), {
//   name: '',
// })

</script>

表示例

親のボタンを押すと
親/子で両方の値が変わるのが分かる

image
image

NOTE

ただしこの書き方では子コンポーネントで値を変えるということが出来なかった

環境

  • Nuxt 3.2.2

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

2
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
2
2