3
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 3 years have passed since last update.

Vue.jsで条件に合わせて属性を変更する

Last updated at Posted at 2020-07-08

例えばCSSフレームワークbulmaでボタンをクリックできなくする属性disabledを条件によって分岐したい場合でやってみる。
disabled === true のときはボタンが押せなくなり、falseのときは押下可能。
参考: https://bulma.io/documentation/elements/button/

.vue
<template>
  <div>
    <button class="button" v-bind="buttonAttr"></button>
  </div>
</template>

computedでif文や三項演算子で条件分岐してreturnすればよい。
例えばbtnFlgというdataを用意して、それのbool値で判断する場合。

.vue
export default {
  computed: {
    buttonAttr() {
      return this.btnFlg
        ? { disabled: true }
        : { disabled: false }
    }
  }

もちろん、よく使うclassやhrefなどどんな属性でもこんな風に書けるから結構便利。

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