我正在使用 Yeoman 的 angular-fullstack发电机。
我想访问前端 Controller (main.controller.js
) 中的一些环境变量。我知道如何设置环境变量的唯一方法如下:
server/config/environment/local.env.js
'use strict';
// Use local.env.js for environment variables that grunt will set when the server starts locally.
// Use for your api keys, secrets, etc. This file should not be tracked by git.
//
// You will need to set these on the server you deploy to.
module.exports = {
DOMAIN: 'hidden',
SESSION_SECRET: "hidden",
// Control debug level for modules using visionmedia/debug
DEBUG: 'hidden',
CALENDAR_ID: 'hidden',
API_KEY: 'hidden'
};
但是,据我了解,这只能在服务器上访问它,但我希望它在前端。以下内容在我的前端 Controller 中不起作用:ENV.API_KEY
和 process.env.API_KEY
。
我应该做什么?
最佳答案
这是服务器端文件,所以,你需要让客户端(浏览器)可以访问这个文件,你需要确认这个文件位于客户端可以直接访问的公共(public)路径中,就像一般的web一样..你可以使用标签来加载这个源文件,其他文件可以依赖它。
关于javascript - 使用 Yeoman 的 Angular-fullstack 生成器时,如何访问前端的环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28357821/