main.js中没有可用的环境变量

时间:2019-07-06 22:10:22

标签: javascript vue.js environment-variables

我使用Vue-CLI 2.9.6创建了项目,现在我想使用.env中的API_URL,但在main.js中不可用。如何在main.js中使用.env中的API_URL?

代码:

import 'bootstrap-vue/dist/bootstrap-vue.css';
import axios from 'axios';

import Vue from 'vue';
import BootstrapVue from 'bootstrap-vue';
import App from './App';
import router from './router';
import store from './store/store';
import Logger from './utils/Logger';

Vue.use(BootstrapVue);

Vue.config.productionTip = false;

const baseAxios = axios.create({
  baseURL: process.env.API_URL,
});

Vue.prototype.$http = baseAxios;

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>',
});

我需要使用API​​_URL,因为我想在axios上设置baseUrl。

1 个答案:

答案 0 :(得分:0)

  

只有以VUE_APP_开头的变量将通过webpack.DefinePlugin静态嵌入到客户端捆绑包中。您可以在应用程序代码中访问它们:

https://cli.vuejs.org/guide/mode-and-env.html#using-env-variables-in-client-side-code