[英]Angular2 AOT Compilation with static external js file
Directed by the answer to my question about Server-side rendering , I'm looking at implementing AOT compilation in my Angular2 app. 在回答有关服务器端渲染的问题时 ,我正在考虑在Angular2应用程序中实现AOT编译。
A bit of background to the problem I'm having: we have a build running in Visual Studio Online, which runs webpack etc, and ships a functioning webapp. 我遇到的问题有一些背景知识:我们有一个在Visual Studio Online中运行的版本,该版本运行webpack等,并提供了一个正常运行的webapp。 We then have a release in VSO to different environments which puts some values (environment, base urls, important keys) into an
env-config.js
file. 然后,我们在VSO中发布了一个针对不同环境的版本,该版本将一些值(环境,基本url,重要键)放入
env-config.js
文件中。 This env-config.js
file is not bundled and packaged within the app, but we access it as a global js variable within our Angular code. 此
env-config.js
文件未在应用程序内捆绑和打包,但我们在Angular代码中将其作为全局js变量进行访问。
env-config.js ENV-config.js
var envConfig = {
'environment': 'local',
'baseApiUrl': 'localhost:5555',
}
Within app-config.ts
, we access envConfig
from the window object, store this as a constant, and export the constant AppConfig
, which we then register in app.module
using an OpaqueToken
. 在
app-config.ts
,我们从窗口对象访问envConfig
,将其存储为常量,然后导出常量AppConfig
,然后使用OpaqueToken
在app.module
注册。
app-config.ts APP-config.ts
export function getEnvConfig(): IAppEnvConfig {
if (window == null) {
return {
'environment': '',
'baseApiUrl': ''
};
}
else {
return (window as any).envConfig;
}
}
export const _envConfig: IAppEnvConfig = getEnvConfig();
export const AppConfig: IAppConfig = {
applicationName: 'Web Application',
environment: _envConfig.environment,
baseApiUrl: _envConfig.baseApiUrl,
otherSetting: 'someValue'
}
This works really well running in the browser with the JIT compiler. 这在使用JIT编译器的浏览器中运行非常好。 I'm following a combination of this and this and other tutorials to enable AOT compilation.
我正在结合使用本教程和本教程以及其他教程来启用AOT编译。
Running ngc gives me the following error: 运行ngc给我以下错误:
"node_modules/.bin/ngc" -p app/tsconfig-aot.json Error encountered
resolving symbol values statically. Calling function 'getEnvConfig',
function calls are not supported. Consider replacing the function or lambda
with a reference to an exported function, resolving symbol AppConfig
I added the check for window == null
in getEnvConfig()
because I don't think window
will be available during non-browser compilation. 我在
getEnvConfig()
添加了window == null
的检查,因为我认为非浏览器编译期间window
不会可用。 If getEnvConfig()
does anything other than return an empty IAppEnvConfig
object I get the ngc error. 如果
getEnvConfig()
除了返回一个空的IAppEnvConfig
对象,除执行其他操作IAppEnvConfig
我将收到ngc错误。
I have done a lot of Googling, but have found nothing specific to my issue, other than generic answers pointing towards using a factory function to create a new instance of a class, which I've tried to do here. 我已经做了很多Google搜寻工作,但是除了针对使用工厂函数创建类的新实例的通用答案之外,我没有发现任何针对我的问题的特例,这是我在这里尝试过的。
Sorry if that doesn't make much sense - please feel free to get clarification/tell me that I'm doing something incredibly stupid. 抱歉,如果这没有太大意义-请随时向我澄清/告诉我我做的事情非常愚蠢。
Thanks in advance, Alex 预先感谢Alex
I'm not sure if this solution will fit you, but I post it any way. 我不确定该解决方案是否适合您,但我会以任何方式发布。 I faced the same issue and foud the problem was quite easy to solve.
我遇到了同样的问题,觉得这个问题很容易解决。 Simply put your functions in a class.
只需将您的函数放在一个类中。 Something like this:
像这样:
export class AppConfig {
_envConfig = AppConfig.getEnvConfig();
AppConfig = {
applicationName: 'Web Application',
environment: this._envConfig.environment,
baseApiUrl: this._envConfig.baseApiUrl,
otherSetting: 'someValue'
};
static getEnvConfig() {
if (window == null) {
return {
'environment': '',
'baseApiUrl': ''
};
} else {
return (window as any).envConfig;
}
}
private constructor(){}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.