How to declare global variable in Vue JS?

By Hardik Savani | September 21, 2019 | Category : Vue.JS

In this example, i want to share with you how to set global variable in vue js. so you can get global variable value and use global variable in vue js. if you define global variable then you can easily access global variable in vuejs app.

We will define global variables using vue js mixin. using mixin we will declare all variables in data method. then we will use in our app vue object.

Here, we will create simple example of declaring vue js global variable. i will create "myGlobalVar" variable with some dummy text on vue js mixin. then we will use "myGlobalVar" variable in our vue js app main object and their method.

So you can see bellow full example:


<!DOCTYPE html>



<title>How to declare global variable in Vue JS? -</title>

<script src=""></script>



<div id="app">

<p>Result : {{myResult}}</p>

<button @click="clickFunction()">Click Me</button>


<script type="text/javascript">


data: function() {

return {

myGlobalVar:'this is my'




var app = new Vue({

el: '#app',

data: {

myResult: ''



clickFunction: function () {

this.myResult = this.myGlobalVar;








Result : this is my

I hope it can help you...

Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: