LoginSignup
2
1

More than 3 years have passed since last update.

Vue.jsのthis変数を可変的、動的に変更する

Last updated at Posted at 2020-04-03

Vue.jsのthis変数を可変的に変更したいときがあったけども、ぐぐっても出てこなかったのでメモ。

.を付けずに、[]を使用して表現する。
javascriptでObjectを扱うのと同じですね。

vue.js
data() {
  return {
    isCheck: false
  }
},
methods: {
  CheckFunc: arg => {
    // 正解例
    // this.isCheckと同義
    this[arg] = true;

    // NG例
    this.[arg] = true;
  }
}

実は公式リファレンスにサラッと書いてある。
https://jp.vuejs.org/v2/cookbook/adding-instance-properties.html

2
1
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
2
1