当我尝试在启动app.component之前加载环境配置时,首先我使用HttpClient http.get()从资产中加载environment.json:
service
init() {
const url = 'assets/config/environment.json';
return this.http.get<any>(url);
}
app.module
export class AppModule implements DoBootstrap {
// ...
ngDoBootstrap(appRef: ApplicationRef) {
this.commonService.init().subscribe(env => {
console.log(`env init`, env);
appRef.bootstrap(AppComponent);
// ...
});
它没有用。
然后我改用fetch(),它可以工作.....但是我不知道为什么
service
init() {
return from(
fetch('assets/config/environment.json', {
method: 'GET'
})
);
}
app.module
export class AppModule implements DoBootstrap {
// ...
ngDoBootstrap(appRef: ApplicationRef) {
this.commonService.init().subscribe(env => {
console.log(`env init`, env);
appRef.bootstrap(AppComponent);
// ...
});
Angular CLI: 8.3.5
Node: 12.12.0
OS: win32 x64
Angular: 8.2.7
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.803.6
@angular-devkit/build-angular 0.803.6
@angular-devkit/build-optimizer 0.803.6
@angular-devkit/build-webpack 0.803.6
@angular-devkit/core 8.3.5
@angular-devkit/schematics 8.3.5
@angular/cdk 8.2.1
@angular/cli 8.3.5
@ngtools/webpack 8.3.6
@schematics/angular 8.3.5
@schematics/update 0.803.5
rxjs 6.5.3
typescript 3.5.3
webpack 4.39.2
Fetch不需要导入任何库即可使用Angular来说明它为什么起作用。
对于Http,您必须将HttpClient
要在服务中app.module
导入的用户也导入HttpClientModule
。
您能提供更多信息吗?所以我们会帮助喜欢
-在完成什么的进口
service
和app.module
?
-控制台中是否有任何错误?
你也可以参考这个
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句