0
3

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.js】@clickの中に条件式を書けばシンプル

Last updated at Posted at 2022-01-24

環境:Vue2

※この記事は初学者が書いています。おかしな部分などありましたら教えて頂けると嬉しいです※

「ボタンを押すとターゲットの表示/非表示が切り替えられる」といった簡単な処理であれば
メソッドを定義せずに@clickの中に条件式を書いたほうがシンプル。というだけの記事です。
もちろん、記述量が少ない=良いコード、というわけではないので状況や考え方によるとは思いますが。

methodsに記述したver

See the Pen button click with methods by monji (@monji88) on CodePen.

@clickの中に式を記述したver

See the Pen button click without methods by monji (@monji88) on CodePen.

ちなみに

当たり前なのかもしれませんが、
@clickの中には下記のように三項演算子を書いたり複数の式を書いたりもできます。

<button
   @click="hoge = hoge == article.id ? null : article.id;
           isActive = false;"
>ボタン</button>
0
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?