将环境变量加载到vue.js中 [英] Load environment variables into vue.js

查看:237
本文介绍了将环境变量加载到vue.js中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用node.js + vue.js构建应用程序,并且想知道是否有人知道如何将环境变量加载到我的vue.js组件中?现在,我正在使用dotenv包在服务器端代码中加载环境变量,但它似乎不适用于vue.js.

I am building an app using node.js + vue.js and was wondering if anyone knows how I can load environment variables into my vue.js components? Right now I'm loading environment variables in my server side code using the dotenv package but it doesn't seem to work for vue.js..

提前谢谢!

推荐答案

您可以像这样从NodeJS公开环境变量:

You can expose environment variables from NodeJS like this:

console.log(process.env.EXAMPLE_VARIABLE);

有关process.env的更多详细信息: https://nodejs.org/api/process.html #process_process_env

More details on process.env: https://nodejs.org/api/process.html#process_process_env

要将环境变量暴露给Vue(或与此相关的任何Javascript框架)中的客户端,您可以呈现来自NodeJS的JSON响应,该响应是通过AJAX调用提取的.

To expose your environment variables to the client-side in Vue (or any Javascript framework for that matter), you could render a JSON response from NodeJS that is ingested via an AJAX call.

这篇关于将环境变量加载到vue.js中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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