简体   繁体   English

在 Nuxt.js 中使用 Netlify 环境变量

[英]Using Netlify environment variables with Nuxt.js

I want my web app, that's built with Nuxt.js and Vue.js to access the environment variables that I added in the Netlify environment UI (setting menu).我希望使用 Nuxt.js 和 Vue.js 构建的 Web 应用访问我在 Netlify 环境 UI(设置菜单)中添加的环境变量。

When I run the local server I have no problem with my code accessing the .env file and retrieving the right codes.当我运行本地服务器时,我的代码访问 .env 文件并检索正确的代码没有问题。

But when I build it with Netlify, it returns undefined.但是当我用 Netlify 构建它时,它返回 undefined。

I have tried adding different prefixes to the env variables since I saw some people do this:我曾尝试向 env 变量添加不同的前缀,因为我看到有些人这样做:

VUE_APP_MY_VARIABLE
NUXT_APP_MY_VARIABLE
VUE_MY_VARIABLE
NUXT_MY_VARIABLE

I do not understand why the variables can not be accessed when the site is live.我不明白为什么在网站上线时无法访问这些变量。

If you're missing any information, let me know and I will add it.如果您缺少任何信息,请告诉我,我会添加它。

Thanks in advance!提前致谢!

Maybe this page can help you!也许这个页面可以帮助你! https://nuxtjs.org/docs/2.x/configuration-glossary/configuration-env https://nuxtjs.org/docs/2.x/configuration-glossary/configuration-env

Try:尝试:

NUXT_ENV_MY_VARIABLE 

instead of代替

NUXT_MY_VARIABLE

I use Netlify and Nuxt together with dotenv package:我将 Netlify 和 Nuxt 与 dotenv 包一起使用:

"@nuxtjs/dotenv": "1.4.1",

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

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