3
1

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 と computed の違いと method の作用が分かる簡単なコードの例

Last updated at Posted at 2020-05-05

NOTE

  • computed は計算機能付き props である

算出プロパティ ※計算機能付きのプロパティ
データそのものに何らかの処理を与えたものをプロパティにしたい(インスタンスに持たせて参照できるようにしたい)ときに用いる

Code

props の値を二倍するだけの computed の例
count を二倍した countDouble を作っている

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

<script>
export default {
  data () {
    return {
      count: 1
    }
  },
  computed: {
    countDouble () {
      return this.count * 2
    }
  },
  methods: {
    incrementCounter (e) {
      this.count = this.count + 1
    }
  }
}
</script>

// https://dev83.com/vue-computed-methods/

View

count の値を増やすと doubleCount も増える

image
image

computed vs method

method でも同じことが実現できるけど
computed にはキャッシュの利点があるようです

image

Original by Github issue

チャットメンバー募集

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

Twitter

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?