Angular 2使用Promise/Observable中的值设置APP_BASE_HREF [英] Angular 2 Set APP_BASE_HREF with a value from a Promise / Observable
问题描述
我尝试使用异步rest调用中的值在"CoreModule"中设置APP_BASE_HREF. 我看不到如何完成此操作,因为Provide方法需要返回一个字符串.
I try to set the APP_BASE_HREF in the "CoreModule" with a value from a async rest call. I can't see how this is done, because the provide method needs to return a string.
例如:
@NgModule({
imports: [
...
HttpModule
],
...
providers: [
...
...
BackendRequestClass,
{ provide: APP_BASE_HREF, useFactory: () => () => return '/some/path', deps: [], multi: true }
],
});
但是当我需要Web服务中的值时,无法返回该字符串. 有什么想法可以做到吗?
but when I need the value from a webservice, I can't return the string. Any ideas how this could be done?
thx
推荐答案
我尝试了您的解决方案. 问题是,当时
I tried your solution. The problem is, that at the time
{ provide: APP_BASE_HREF, useFactory: (config) => config.appBaseHref, deps: [ConfigService] }
尚未设置config.appBaseHref.
当我调试代码时,看到APP_INITIALIZER
是在APP_BASE_HREF
the config.appBaseHref is not set yet.
When I debug the code I see, that the APP_INITIALIZER
is executed after the provider from APP_BASE_HREF
这导致未设置BASE_HREF
.
这篇关于Angular 2使用Promise/Observable中的值设置APP_BASE_HREF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!