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.
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.
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.
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.
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()
andthis.$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>
This is not a problem with Vue.
You can create global functions in javascript using something like:
window.gFun = window.gFun || () => console.log("Works");
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With