简体   繁体   English

main.js中不提供环境变量

[英]Enviroment variables not available in main.js

I created project with Vue-CLI 2.9.6 and now I would like use API_URL from .env but it isn't available in main.js. 我使用Vue-CLI 2.9.6创建了项目,现在我想使用.env中的API_URL,但在main.js中不可用。 How can I use API_URL from .env in main.js? 如何在main.js中使用.env中的API_URL?

Code: 码:

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/>',
});

I need use API_URL because I would like set baseUrl on axios. 我需要使用API​​_URL,因为我想在axios上设置baseUrl。

Only variables that start with VUE_APP_ will be statically embedded into the client bundle with webpack.DefinePlugin. 只有以VUE_APP_开头的变量将通过webpack.DefinePlugin静态嵌入到客户端捆绑包中。 You can access them in your application code: 您可以在应用程序代码中访问它们:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM