Javascript 在AoT编译器中导出变量
我尝试实现动态配置,如图所示 一切都在JiT编译器中工作,但我得到Javascript 在AoT编译器中导出变量,javascript,angular,angular-aot,Javascript,Angular,Angular Aot,我尝试实现动态配置,如图所示 一切都在JiT编译器中工作,但我得到 ERROR in Error during template compile of 'environment' Function calls are not supported in decorators but 'Environment' was called. 尝试使用AoT编译器生成时 这是我的环境。ts(注意类环境已导出): 我仍然希望以标准方式使用环境some.component.ts: import { env
ERROR in Error during template compile of 'environment'
Function calls are not supported in decorators but 'Environment' was called.
尝试使用AoT编译器生成时
这是我的环境。ts
(注意类环境已导出):
我仍然希望以标准方式使用环境some.component.ts
:
import { environment } from '../environments/environment';
console.log(environment.config.property);
@Component(...)
export class SomeComponent {
constructor(private configService: ConfigService) { }
private myMethod() {
console.log(this.configService.get.property);
}
}
不要。说真的,远离这两个文件(
environment.ts
和environment.prod.ts
)。这些不是关于“环境”这个词的DevOps含义,而是关于调试常量
如果需要知道是否正在运行调试生成,请导入:
如果您需要动态配置,只需从某处读取Json,或者让服务器端将其作为脚本标记注入,然后直接或通过依赖项注入读取它(这并不难做到)
但别把那些文件弄乱了。相信我,你以后会感谢我的;) 通过创建
config.module.ts
和config.service.ts
解决了这个问题。配置模块声明提供程序:
@NgModule({
providers: [
ConfigService,
{
provide: APP_INITIALIZER,
useFactory: (appConfigService: ConfigService) => () => appConfigService.loadAppConfig(),
deps: [ConfigService],
multi: true,
},
],
})
export class ConfigModule {}
在some.component.ts
中使用配置服务:
import { environment } from '../environments/environment';
console.log(environment.config.property);
@Component(...)
export class SomeComponent {
constructor(private configService: ConfigService) { }
private myMethod() {
console.log(this.configService.get.property);
}
}
对于测试,将导入json测试配置文件:
import { default as appTestConfig } from '../../../../assets/app-config.test.json';
并直接在配置服务上设置:
TestBed.configureTestingModule({
...,
imports: [
ConfigModule,
...
],
providers: [
{
provide: APP_INITIALIZER,
useFactory: (appConfigService: ConfigService) => () => appConfigService.setConfig(appTestConfig),
deps: [ConfigService],
multi: true,
},
]
}).compileComponents();