LoginSignup
8
7

More than 3 years have passed since last update.

Vueの子コンポーネントでpropsの変更を検知する

Posted at
parent.vue
<template>
  <childComponent name="foo">
</template>
// 略
child.vue
<template>
  <div>{{ awesomeName }}</div>
</template>
<script>
export default {
  props: {
    name: String
  },
  data () {
    return {
      awesomeName: this.name
    }
  }
</script>

こういう時、親から渡す値が変更された際に検知するにはpropsをwatchで監視する。

child.vue
// 略
<script>
export default {
// 略
    watch {
      name (newName) {
        this.awesomeName = newName;
      }
    }
// 略
</script>

おういえ🤩

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