6
6

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】computedで引数を渡す方法

Last updated at Posted at 2020-11-05

computedプロパティに引数を渡す方法。

通常、computedに引数を渡すことができないが、computedの処理の中に関数を書くことで引数を渡すことができる。

通常
export default {
  computed:{
    変数名(){
      return 処理
    } 
  }
}
引数あり
export default {
  computed:{
    変数名(){
      return (引数)=>{
        処理
      }
    } 
  }
}

ややこしそうに見えるが、
return 処理
return (引数)=>{処理}
としただけ。

追記

変更がある度に自動で再計算をして欲しかったのでcomputedプロパティを使ったが、methodsプロパティに置き換えても全く同じ処理ができた。。(上級者に教えてもらいました)

また、computedは中の処理が他の変数と依存関係にある場合のみ、変更を検知して自動更新する処理となる。

このため、computedの中に依存関係がない場合値は更新されない。

都度、新しい処理を実行する場合はメソッドを使う。

結論
引数を渡したい場合はmethodsでいけます。わざわざcomputedを使う必要なし。

6
6
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
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?