Advertisement
StraMa87

Vue js external config file json

Jan 11th, 2022 (edited)
2,140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. struttura vue
  4. src/http-call/http-interceptor.js
  5.  
  6. */
  7.  
  8. /*----------------------- http-interceptor.js -----------------------*/
  9. import axios from "axios";
  10. import { API } from "../../public/config.json"; // questo è quello che usavo prima, però per cambiare il valore devo buildare di nuovo
  11. import Vue from "vue";                                              // il progetto e io devo poter cambiare questo e altri valori anche
  12.                                                                     // se l'applicazione è già in produzione
  13.  
  14. /*
  15.  Qui è dove il Vue.prototype.$configJson è undefined
  16. */
  17.  
  18. export const http = axios.create({
  19.   baseURL: API,
  20.   headers: {
  21.     "Content-Type": "application/json",
  22.     "Access-Control-Allow-Origin": "*",
  23.   },
  24. });
  25.  
  26. /*
  27.  
  28. importo questa costante in tutti i file in cui ho bisogno di effettuare chiamate http.
  29.  
  30. */
  31.  
  32. /*----------------------- main.js -----------------------*/
  33.  
  34. il file config è nella cartella public/
  35. axios
  36.   .get(`${process.env.BASE_URL}config.json`)
  37.   .then((resp) => {
  38.     Vue.prototype.$configJson = resp.data;
  39.     new Vue({
  40.       store,
  41.       router,
  42.       render: (h) => h(App),
  43.     }).$mount("#app");
  44.   })
  45.   .catch((err) => console.log(err));
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement