0 votes
in VueJS by

What are the different ways to create filters in VueJS?

1 Answer

0 votes
by

You can define filters in two ways,

  1. Local filters: You can define local filters in a component’s options. In this case, filter is applicable to that specific component.
filters: {
  capitalize: function (value) {
    if (!value) return ''
    value = value.toString()
    return value.charAt(0).toUpperCase() + value.slice(1)
  }
}
  1. Global filters: You can also define a filter globally before creating the Vue instance. In this case, filter is applicable to all the components with in the vue instance,
Vue.filter('capitalize', function (value) {
  if (!value) return ''
  value = value.toString()
  return value.charAt(0).toUpperCase() + value.slice(1)
})

new Vue({
  // ...
})
...