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の基礎勉強 v-on

Last updated at Posted at 2023-11-09

(Vue.js 3.1.5を使用)

ボタンをクリックしたときにイベント処理したいなどの時。

v-onディレクティブというものがある。
v-on:イベント名 = "メソッド名"
という構文を使う。

ボタンをクリックすると、コンソールにクリック!と表示される処理。

html
<div id="app">
    <button v-on:click="onClick">クリック!</button>
</div>

v-on:click="onClick" と書いたので、
javascript側の、onClickメソッドが呼ばれる。

javascript
const app = Vue.createApp({
    methods:{
        onClick: function() {
            console.log('クリック!')
        }
    }
})
app.mount('#app')

1.PNG

ボタンを2回クリックしたから、コンソールに、②クリック!と出ている。

追記

v-on:click は、@clickとも書ける。

html
<div id="app">
    <button @click="onClick">クリック!</button>
</div>
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?