访问webpack AngularJS项目中的env变量 [英] Accessing env variables inside webpack AngularJS project

查看:228
本文介绍了访问webpack AngularJS项目中的env变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AngularJS项目,它使用webpack进行捆绑,服务和构建 - 不使用诸如bower或gulp之类的任务运行。我想要能够设置环境变量,例如我在本地和生产中消耗的REST API端点,然后访问我实际的AngularJS项目文件中的那些,特别是在控制器内部。定义和传递这些env变量到项目中的最好方法是什么?

解决方案

解决方案是使用Webpack的definePlugin来定义自由/全局变量:



webpack.github.io/docs/list-of-plugins.html#defineplugin

  new webpack.DefinePlugin({
VERSION:JSON.stringify(5fa3b9),
BROWSER_SUPPORTS_HTML5:true,
TWO:1 + 1 ,
typeof window:JSON.stringify(object)
})


I have an AngularJS project that uses webpack for bundling, serving, and building - no task runner such as bower or gulp is used. I'd like to be able to set environment variables for things such as the REST API endpoints that I'll be consuming in local versus production, and then access those within my actual AngularJS project files, particularly inside controllers. What's the best way to define and pass these env variables into the project?

解决方案

The solution was to use Webpack's definePlugin to define free/global variables:

webpack.github.io/docs/list-of-plugins.html#defineplugin

new webpack.DefinePlugin({
    VERSION: JSON.stringify("5fa3b9"),
    BROWSER_SUPPORTS_HTML5: true,
    TWO: "1+1",
    "typeof window": JSON.stringify("object")
})

这篇关于访问webpack AngularJS项目中的env变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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