0
0

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.

#Vue で props が文字列として component で受け渡されてしまう ( props as string or integer / increment example)

Last updated at Posted at 2020-05-05

解決

コロン : をつけて bind で 子component に props を渡す

<template>
  <div>
-    <Counter count="1"/>
-    <Counter count="2"/>
-    <Counter count="3"/>
+    <Counter :count="1"/>
+    <Counter :count="2"/>
+    <Counter :count="3"/>
  </div>
</template>

bind じゃなくてもとりあえず props を渡せているようだけど、これは何なのだろう

Code

pages/counter-three.vue

<template>
  <div>
    <Counter count="1"/>
    <Counter count="2"/>
    <Counter count="3"/>
  </div>
</template>

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

export default {
  components: {
    Counter
  }
}
</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>

インクリメントすると文字が増えてしまう

image

Original by Github issue

チャットメンバー募集

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

Twitter

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?