6
2

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

Vue functional componentでのmethods,filter

Last updated at Posted at 2019-12-28

functional componentの基本については以前記載したので、
今回はfilterとmethodについてまとめます。

filter

通常のcomponent同様

<template functional>
  <div>
    {{ props.num | yen }}
  </div>
</template>

<script>
export default {
  props: {
    num: { type: Number, default: 0 }
  },
  filters: {
    yen: function(str) {
      return str + "yen";
    }
  }
};
</script>

method

$optionsを通してアクセスできる。

通常のcomponentと同じように「methods:{}」とオブジェクトの中に定義しても問題ないが、テンプレートで「$options.methods.XXX()」となって見づらくなるだけのため直接。
またmethodの中で「this.XXX(props)」はできないため、使用するならmethodの引数として渡す。

<template functional>
  <div>
    {{ $options.double(props.num) }}
  </div>
</template>

<script>
export default {
  props: {
    num: { type: Number, default: 0 }
  },
  double: function(num) {
    return num * 2;
  }
};
</script>
6
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?