42

In my Vue.js application I want to have some global functions. For example a callApi() function which I can call every time I need access to my data.

What is the best way to include these functions so I can access it in all my components?

  • Should I create a file functions.js and include it in my main.js?
  • Should I create a Mixin and include it in my main.js?
  • Is there a better option?
4
  • Maybe Vuex? vuex.vuejs.org/en/data-flow.html Commented Sep 7, 2016 at 14:49
  • 3
    Vuex us mainly to store data if I'm right? I don't think it is for creating global functions? Commented Sep 7, 2016 at 14:50
  • My thought was that you "need access to your data"; Vuex would provide that. Vuex itself can make the calls that fetch the data. Commented Sep 7, 2016 at 15:05
  • Thank you. Perhaps my question was unclear, sorry. I meant global functions. Commented Sep 7, 2016 at 15:38

4 Answers 4

43

I have a file with function like a func.js like below

export const func = {
   functionName: (data) => {
      return something  
    }
}

In main.js add 2 string

import {func} from './func.js'

Vue.prototype.$func = func

and you can use from all components if in script tag like below

this.$func.functionName(somedata)

or if template tag like

$func.functionName(somedata)
Sign up to request clarification or add additional context in comments.

1 Comment

Like this answer since it's quite clear about "what" and "how" should I get it done.
36

Your best bet would be a Plugin, which lets you add features to the global vue system.

[from the vuejs Docs]

MyPlugin.install = function (Vue, options) {

// 1. add global method or property
Vue.myGlobalMethod = ...

// 2. add a global asset
Vue.directive('my-directive', {})

// 3. add an instance method
Vue.prototype.$myMethod = ...

}

Then you would just add

Vue.use(MyPlugin)

in your code before calling your function.

Vue.myGlobalMethod(parameters);

or in your case

Vue.callApi(parameters);

5 Comments

Thank you. What exactly is the difference between Mixins and Plugins (for adding global functions)?
@Jordy the difference is that a mixin must be included in your mixin hash in your vm definition for each component that requires that functionality. A plugin makes it globally available in all your components, without requiring inclusion (since it extends the main Vue instance, each component you create already includes this functionality)
@Jordy Tremendus has it right, Mixins are limited functionality that's added to components separately which is what I'd normally use unless a large majority of my components need that function.
How to create plugin with webpack?
Is it the same process when using Nuxt?
7

Mixins can be registered globally​ too. https://v2.vuejs.org/v2/guide/mixins.html#Global-Mixin

Comments

1

Very simle way is to declare function in window (if is web aplication)

window.myFunc = () => {
    console.log("Hello")
}

...

new Vue({
  el: "#app",
  mounted() {
      myFunc()
  }
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.