LoginSignup
0
0

More than 1 year has passed since last update.

v-onディレクティブ(2)

Last updated at Posted at 2021-05-05

Viewインスタンス

全てのVueアプリケーションは、Vue 関数で新しい Vue インスタンスを作成することによって起動されます。
Vueインスタンスは、データ監視、イベント通信や DOM 操作をするいくつかのインスタンスメソッドを持っています。

var vm = new Vue({
  // オプション
})

関数とは

同じ処理を定義し何度も使い回しができる形にしたもの。

<script>
  function 関数名(){
  処理
  }
</script>

無名関数とは

関数名なしで関数を定義できるもの。

<script>
  function(){
  処理
  }
</script>

v-on:clickをvueインスタンス内で定義してみます

<body>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.6.12/dist/vue.js"></script>
  <div id="app">
      <p>現在{{ number }}回クリックされています</p>
      <button v-on:click="countUp">カウントアップ</button>
</div>
<script>
  new Vue({
    el: '#app',
      data: { 
          number: 0
        },
      methods: {
        countUp: function(){
          this.number += 1
        }
      }
  })
</script>
</body>

20210505-130744.png

【参考】関数呼び出しパターンのthis

呼び出し元
number: 0

*data: { }内

呼び出し先
this.number += 1

*methods{無名関数function( ){ }内}

@hiroshimaeasyryo様
https://qiita.com/hiroshimaeasyryo/items/a270b00c420ed96f02f0

@Sthudent_Camilo
https://qiita.com/Sthudent_Camilo/items/29467821eac0a11cfb80

@takkyun
https://qiita.com/takkyun/items/c6e2f2cf25327299cf03

JavaScript Primer
https://jsprimer.net/basic/function-this/

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