Vue-cli 3环境变量均未定义 [英] Vue-cli 3 Environment Variables all undefined

查看:86
本文介绍了Vue-cli 3环境变量均未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了所有解决方案,但似乎没有一个适合我.我只想将一些值存储在Vue应用程序内的 .env 文件中,但是只是尝试登录 process.env 会从组件内部返回一个空对象.

I've tried all of the solutions out there but none seem to work for me. I just want to store some values in a .env file within my Vue app but simply trying to log process.env returns an empty object from within the component.

我的 .env 文件

VUE_APP_URL={api url}
VUE_APP_TOKEN={token}

我的计划是将这些环境变量设置为数据属性,但它始终返回 undefined .如果我从webpack.config.js中执行 console.log(process.env.NODE_ENV),它将显示我正在开发中,但是如果我尝试在类似组件中进行同样的操作

My plan was to set these environment variables to data properties but it always returns undefined. If I do console.log(process.env.NODE_ENV) from webpack.config.js it will show that I'm in development but if I tried doing the same from within the component like

mounted() {
    this.$nextTick(() => {
      console.log(process.env.VUE_APP_URL);
    })
  }

它只返回 undefined .

推荐答案

我知道了-我必须安装 dotenv-webpack 并在webpack.config.js中对其进行初始化,这很奇怪,因为没有的文档指出我需要这样做.

I figured it out - I had to install dotenv-webpack and initialize it in webpack.config.js which is odd because none of the docs stated that I needed to do so.

这篇关于Vue-cli 3环境变量均未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆