【发布时间】:2017-10-10 14:21:20
【问题描述】:
根据my question about Server-side rendering 的答案,我正在考虑在我的 Angular2 应用程序中实现 AOT 编译。
我遇到的问题的一些背景知识:我们在 Visual Studio Online 中运行了一个构建,它运行 webpack 等,并发布了一个正常运行的 webapp。然后,我们在 VSO 中发布到不同环境的版本,将一些值(环境、基本 url、重要键)放入 env-config.js 文件中。这个env-config.js 文件没有在应用程序中捆绑和打包,但我们在 Angular 代码中将其作为全局 js 变量进行访问。
env-config.js
var envConfig = {
'environment': 'local',
'baseApiUrl': 'localhost:5555',
}
在app-config.ts 中,我们从窗口对象访问envConfig,将其存储为常量,然后导出常量AppConfig,然后我们使用OpaqueToken 将其注册到app.module。
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'
}
这在带有 JIT 编译器的浏览器中运行得非常好。我正在关注this 和this 的组合以及其他教程来启用AOT 编译。
运行 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
我在getEnvConfig() 中添加了对window == null 的检查,因为我不认为window 在非浏览器编译期间可用。如果getEnvConfig() 除了返回一个空的IAppEnvConfig 对象之外,我会得到 ngc 错误。
我已经做了很多谷歌搜索,但没有发现任何特定于我的问题的问题,除了指向使用工厂函数创建类的新实例的通用答案,我在这里尝试过这样做。
对不起,如果这没有多大意义 - 请随时澄清/告诉我我正在做一些非常愚蠢的事情。
提前致谢, 亚历克斯
【问题讨论】:
标签: angular angular2-aot