Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create global functions in Vue.js?

I would like to use some functions globally in my app.
Most of the answers for my question refer to Vue Mixin. Although I use it, it cant solve my problem. This is my try

Vue.mixin({
    methods:{
        gFun(){
            console.log("Works");
        },
    }
})

const app = new Vue({
    el: '#app',
    data: {

    },
    created(){
        gFun();
    },
    methods: {

    }
});

Vue says "ReferenceError: testMixin is not defined".
I want is just to able to use gFun() globally(not using like this.gFun()). Please explain my something.
Using Vue Mixin or not is ok.

like image 748
Mg Mg Tint Avatar asked Feb 01 '19 02:02

Mg Mg Tint


People also ask

How do you make a global function in Vue?

Using Mixins A Mixin is an object that contains the same options as a component and can be reused and distributed between Vue components. We will create a globalHelper function inside a Mixin and call it from a component.

How do I use Vue mixin?

Mixins are a flexible way to distribute reusable functionalities for Vue components. A mixin object can contain any component options. When a component uses a mixin, all options in the mixin will be “mixed” into the component's own options. console.

Can you use arrow functions in Vue?

Vue ES6 Conventions Use arrow functions for any callbacks inside "top level" methods. Use an arrow function with a "concise body" for component data closures.


3 Answers

One way to go about it is by using the Vue mixins you suggested. Another great way to go about that is by using Plugin.

Notice how I declared them and the difference between how I called the two global variables, especially the dollar sign($) in the plugin option. Both this.gMixinFun() and this.$gPluginFun() will be available as global method in the Vue instance Options.

Vue.config.productionTip = false;
Vue.config.devtools = false;


// OPTION 1: Using Mixin
Vue.mixin({
  methods: {
    gMixinFun: function() {
      return "this is a mixin test"
    }
  }
});

// OPTION 2: Using plugin
const plugin = {
  install() {
    Vue.gPluginFun = () => 'this is a plugin test' //Vue.gPluginFun()
    Vue.prototype.$gPluginFun = () => 'this is a plugin test' //this.$gPluginFun()
  }
}

Vue.use(plugin)

const app = new Vue({
  el: '#app',
  created() {
    console.log("Using mixins =>", this.gMixinFun());
    console.log("Using plugins =>", this.$gPluginFun()); //or you can use Vue.gPluginFun() without the dollar($) sign
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
</div>
like image 166
Solar Avatar answered Oct 28 '22 14:10

Solar


This is not a problem with Vue.

You can create global functions in javascript using something like:

window.gFun = window.gFun || () => console.log("Works");
like image 44
smac89 Avatar answered Oct 28 '22 12:10

smac89


You could declare your global function like function functon_name(parameters){...}, try to pass the vue instance like a parameter in order to access its properties inside the global function as follows

Vue.config.devtools = false;
Vue.config.productionTip = false;

window.gFun = function(vm) {
  console.log(vm.message);
}


const app = new Vue({
  el: '#app',
  data: {
    message: "Hello !"
  },
  created() {
    gFun(this);
  },
  methods: {

  }
})
<link type="text/css" rel="stylesheet" href="//unpkg.com/bootstrap/dist/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>


<div id="app" class="container">

</div>
like image 1
Boussadjra Brahim Avatar answered Oct 28 '22 14:10

Boussadjra Brahim