🖥

#Vue ( nuxt ) – parent and child component props binding example / inc

2023/09/01に公開

Code

pages/counter-three.vue

<template>
  <div>
    <Counter :count="parentCount"/>
    <Counter :count="parentCount"/>
    <Counter :count="parentCount"/>
    <input type="button" @click="incrementParentCounter" value="Increment Parent!">
  </div>
</template>

<script>
import Counter from '~/components/Counter.vue'

export default {
  data () {
    return {
      parentCount: 1
    }
  },
  components: {
    Counter
  },
  methods: {
    incrementParentCounter (e) {
      this.parentCount = this.parentCount + 1
    }
  }

}
</script>

components/Counter.vue

<template>
  <div>
    <h2>
      Count: {{ count }}
    </h2>
    <input type="button" @click="incrementCounter" value="Increment!">
  </div>
</template>

<script>
export default {
  props: {
    count: {
      type: Number,
      default: 1
    },
  },
  methods: {
    incrementCounter (e) {
      this.count = this.count + 1
    }
  }
}
</script>

Example

default

image

increment child counter

image

increment parent counter
child count reseted
because child component props does not bind to parent component

image

Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/3141

チャットメンバー募集

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

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ

Twitter

https://twitter.com/YumaInaura

公開日時

2020-05-05

Discussion