My code uses plugins feature in Vuejs to define global shared variable.
Vue.use(shared)
shared
is defined as:-
export const shared = {
config: getAppConfig()
}
shared.install = function() {
Object.defineProperty(Vue.prototype, '$shared', {
get() {
return shared
}
})
}
function getAppConfig() {
var api = getAPIURL()
return axios.get("https://url/get_config")
.then(response => {
return response.data
}
}
My issue is, in my component, if I use this variable this.$shared.config
, I get undefined
.
Looking at the console window and debug statement, my component code is executed before the plugin got time to this.$shared.config
.
I'm new to javascript+Vuejs but when I researched about this error, it is related to axios being asynchronous so I decided to return promise and use await.
function getAppConfig() {
var api = getAPIURL()
return axios.get("https://url/get_config")
}
But, when in my shared.install
function, I try do:-
shared.install = function() {
let config = await shared.config
I get error: Syntax Error: await is a reserved word
.
Since I'm new, it looks like I'm doing a fundamental error in how I should make this code synchronous. What's the right way to fix this?