Where and how to store API endpoints in vue js? Where and how to store API endpoints in vue js? vue.js vue.js

Where and how to store API endpoints in vue js?


.env files are recommended because you may have different endpoints depending on environment, that is to say are you running dev server with "npm run serve" or building for production with "npm run build". With .env config files they become environment variables and you don't need to hard code them into your app, it's just the most practical thing to do. With Vue CLI 3 you would have

//.env.development VUE_APP_BASEURL = "http://localhost:8000"

And in your app you could access it with.

process.env.VUE_APP_BASEURL

What I use to do is just have the base in a variable and then concatenate rest.

const BASE_URL = process.env.VUE_APP_BASEURLthis.$http.post(BASE_URL + '/api/languages/', params)