LoginSignup
29
17

More than 3 years have passed since last update.

vueでv-modelをネストして使いたい

Last updated at Posted at 2018-05-16
親Component
<template>
  <child-component
    v-model="text"
  />
</template>

<script>
export default {
  data() {
    return {    
      text: ''
    }
  }
}
</script>
child-component
<template>
  <mago-component
    v-model="localValue"
  />
</template>

<script>
export default {
  props: {
    value: {
      type: String
    }
  },
  computed: {
    localValue: {
      get() {
        return this.value
      },
      set(value) {
        this.$emit('input', value)
      }
    }
  }
}
</script>

子Componentでgetterとsetterを指定してあげればよかった。

これで無限にネストできる。

参考
コンポーネントで-v-model-を使う

29
17
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
29
17