9cubed
ブログ | PHP | JavaScript | TypeScript | Vue.js | Laravel | Tailwind | Vite | Python | MariaDB | SQLite | Node.js | Linux | PowerShell | Docker | Git | Web | その他
< 前の記事

Hello, World

次の記事 >

ref オブジェクト

Vue.js

[Vue.js]click イベント

公開日:2026-02-18
更新日:2026-02-18

1. 概要

ボタンが click された時のイベントを拾って処理を実行します。

動作確認:Vite 7.3.1、Vue.js 3.5.25

2. コード

index.html と src/main.ts は、「Hello, World」で作成したものを使用します。

src/App.vue
コード
<script setup lang="ts">

const outputLog1 = ()=> {
  console.log('OK');
}

const outputLog2 = (_event?: MouseEvent)=> {
  console.log(_event);
}

const outputLog3 = (msg:string)=> {
  console.log(msg);
}

const outputLog4 = (_event: MouseEvent, msg:string)=> {
  console.log(_event);
  console.log(msg);
}
</script>

<template>
  クリックすると outputLog1 が呼び出される<br />
  <button v-on:click="outputLog1">Test 1</button><br /><br />
  <button @click="outputLog1">Test 1(v-on:click の省略記法)</button><br /><br />

  引数を省略すると、イベントオブジェクトが渡される<br />
  <button @click="outputLog2">Test 2</button><br /><br />

  引数を指定すると、イベントオブジェクトは渡されない<br />
  <button @click="outputLog3('Test 3')">Test 3</button><br /><br />

  イベントオブジェクトと引数の両方を渡したい場合は、$event と引数を両方指定する<br />
  <button @click="outputLog4($event, 'Test 4')">Test 4</button><br />
</template>
イベントハンドラーを指定する際に、引数を省略すると、自動的にイベントオブジェクトが渡されます。
イベントハンドラーでは受け取らないようにすることもできます。
$event は、イベント発生時に設定される特別な変数です。
< 前の記事

Hello, World

次の記事 >

ref オブジェクト

YouTube X

新着一覧

  • async、awaitJavaScript
  • Promise についてJavaScript
  • パッケージの管理Node.js
  • v-model(双方向バインディング)Vue.js
  • VS Code で GitHub を使ったソース管理Git
  • computed(再計算)Vue.js
  • watchDebounced(値の監視)Vue.js
  • watch(値の監視)Vue.js
  • change イベントVue.js
  • v-memoVue.js

アーカイブ

  • 2026/03
  • 2026/02
  • 2026/01
  • 2025/12
  • 2025/11
  • 2025/10
  • 2025/09
  • 2025/08
  • /00

以前のカテゴリー一覧

  • CakePHP3
  • CentOS7
  • HTML・CSS・JavaScript
  • Haskell
  • JavaScript
  • Kotlin
  • Laravel5
  • PHP
  • Python
  • Ruby
  • RubyOnRails5
  • TypeScript
  • Vue.js
  • Webサーバ講座
  • Webプログラミング講座
  • jQuery
  • linux
  • パソコン講座
  • ブログ
  • プログラミング講座
  • メモ帳作成講座
  • 数学

Copyright © 9cubed. All Rights Reserved.

プライバシーポリシー 利用規約
▲