2

I have to call an API (json-server) to retrieve data in order to populate my dropdowns within my Vue application. Instead of hard-coding within the app, I want to create an external configuration file where I can put any links and eventually put server details for deployment. Currently, I have this but for some reason when I reference the Key in my vue component, it says it undefined. I'm not sure where I'm going wrong.

    /public/config.json
    
   {
     "API_URL": "localhost:3000"
   }

Here is the relevant parts of the main.js file:

    /main.js

    fetch(process.env.BASE_URL+ "config.json")
    .then((json) => {
    json().then((config) => {
    Vue.prototype.$config = config
    new Vue({
     router,
     store,
     render: (h) => h(App)
   }).$mount("#app")
  })
 })

In my Vue component, I'm trying to reference it like this, but it is showing up as undefined.

      <script>
      export default {
      mounted() {
        fetch(this.$config.API_URL)
            .then(res => res.json())
            .then(data => this.mailboxes = data)
            .catch(err => console.log(err.message))
      }
      }

      </script>

If there is another way to go about this please let me know.

2
  • i think .env file is the solution, just make sure that he is added in your .gitignore, for more detais check the officiel doc Commented Jul 27, 2021 at 14:14
  • This question is similar to: Vue js with an external configuration file. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Sep 2, 2024 at 13:41

1 Answer 1

2

If you want the app build once and use same bundle on multiple servers with different configuration, this is right way to do it. ENV variables suggested in comments wont work as the values are "baked in" to the bundle...

However you are using fetch in a wrong way. fetch returns response which you are trying to call instead of calling response.json()

fetch(process.env.BASE_URL+ "config.json")
  .then((response) => response.json())
  .then((config) => {
    Vue.prototype.$config = config
    new Vue({
      router,
      store,
      render: (h) => h(App)
    }).$mount("#app")
  })
Sign up to request clarification or add additional context in comments.

2 Comments

I'm running into an issue where in the fetch code above it says "BASE_URL" is undefined and on the fourth line $config is unused. In my Vue component when I use fetch(this.$config.API_URL), it says API_URL is an unresolved variable. Do I have to define it elsewhere?
Are you using TypeScript? Is this Vue CLI project? Sounds like errors from Vetur/ESLint, not actual runtime errors...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.