Serio
Serio

Reputation: 527

How to make Vue-directive / Vue-filter available in all components of the one page?

please tell me how you can I make the directives / filters available in all components of the one view [vue 2 cli]. Is it right to use a global mixin and write filters/directives there... Or vue-mixins are used only for methods...?

Upvotes: 0

Views: 193

Answers (1)

iikkoo
iikkoo

Reputation: 2856

You can define filters globally before creating the Vue instance (new Vue()).

This is copied from the Vue documentation (https://v2.vuejs.org/v2/guide/filters.html):

Vue.filter('capitalize', function (value) {
  if (!value) return ''
  value = value.toString()
  return value.charAt(0).toUpperCase() + value.slice(1)
})

new Vue({
  // ...
})

To avoid getting to much code in main.js you can also import functions as filters with:

import filterA from '<path>'

Vue.filter('<nameOfFilter>', filterA);

new Vue({
  // ...
})

Upvotes: 1

Related Questions