In a basic web app built using VueJS a call is made to an API that responds with an object containing the environment name. For example:
https://appsdev/mysimpleapp/api/environment
returns
{"applicationName":"My Simple App","version":"1.0.0.0","environment":"DEV"}
Currently, a nav button is set up to conditionally show if the environment is DEV. Here are the pertinent parts of that page:
<template>
<!-- other content here -->
<div @click="updateMenu()" v-show="['DEV'].includes(environment)"><router-link :to="{name: 'dev-notes'}">Dev Notes</router-link></div>
</template>
<script>
// other logic here
data() {
return {
environment: null
}
},
created() {
this.getEnvironment();
},
methods: {
async getEnvironment() {
const environmentDetails = await this.$service.access('environment').get(); // uses Axios to access API
this.environment = environmentDetails.environment;
}
}
}
</script>
While this approach works, there is a noticeable delay in the "Dev Notes" nav button appearing while the app hits the API. Plus, every time the page loads, there is a call to the API.
Looking to improve upon this, it seems like it might be better to hit the API once, when the app initializes, store that value in a variable somewhere and then refer to that variable for conditionally showing the "Dev Notes" nav button. Vuex seems like overkill for such a simple thing and this app doesn't have the .env files that this post describes so how can this be achieved?