Question: how to declare global variable in vue js?, vue global variable undefined, how to load global variable when component instance loads vuejs?, vuejs mounted how to access global variable?
vue global variable
In this Article, We want to share with you how to set & get global variable in vue js. so you can set(declare) and get(fetch) global variable with this data value as well as use global variable in vuejs. if you assign global variable then you can simply Retrive or access global variable in vuejs single page web application.
generally we required to get a describe data all over our app vuejs components in such phase we can create a global variable in vuejs app’s by using this best syntax with global variable this.
so, here where $ is a simple naming convention to assign or declare the global vuejs variables.
Declaring a global variable
In this best example, we are declare global variable a $axios variable which helps us to use the instance axios object in our vuejs components to send the server side http call or requests.
Open you main.js file and add the below code.
import Vue from 'vue'
import App from './App.vue'
import axios from 'axios'
// global variable
Vue.prototype.$axios = axios
render: h => h(App),
Accessing global variable
We can get the global variable from any component instance by using reference global variable from instance vuejs this.$variablename.
I hope you get an idea about How to declare a global variable in Vuejs?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.