create a global function in vue

2

Good afternoon, I'm a beginner in both javascript and vue, and I'm having some difficulty creating a global function in vue. Could someone give me an example? Grate

    
asked by anonymous 04.11.2016 / 20:46

1 answer

2

To add a global method to Vue you can create a plugin:

According to Vue.js - Plugins :

MeuPlugin.install = function (Vue, options) {
  // 1. Adiciona o método global ou propriedade
  Vue.meuMetodoGlobal = ...
  // 2. Adiciona uma diretiva global
  Vue.directive('minha-diretiva', {})
  // 3. Adiciona um método de instância
  Vue.prototype.$meuMetodo = ...
}

Then you add the plugin to Vue:

Vue.use(MeuPlugin)

In your code, to call the method.

Vue.meuMetodoGlobal(parametros);

Reference: Include global functions in Vue.js

    
04.11.2016 / 21:27