gpt4 book ai didi

angular - Angular 模块中的 window.location.origin 为空

转载 作者:行者123 更新时间:2023-12-03 17:22:28 26 4
gpt4 key购买 nike

我有一个应用程序和一个库,两个独立的存储库。我的应用程序使用我的库,并且我的库包含执行 http 请求的服务。
我的应用程序部署到多个环境,我使用 Angular 的 environment.ts 文件来定义外部服务所在的位置。所有应用程序都通过网关提供服务,并位于同一个 window.location.origin 上。
ivy 被禁用,aot 被启用。
我的图书馆公开了一个 forRoot 如下:

  declarations: [
ToggleDirective
],
providers: [
ToggleService,
ToggleServiceConfig
],
exports: [
ToggleDirective
]
})
export class ToggleModule {
static forRoot(config: ToggleServiceConfig): ModuleWithProviders<ToggleModule> {
return {
ngModule: ToggleModule,
providers: [
{provide: ToggleServiceConfig, useValue: config }
]
};
}
}
我的应用程序 environment.ts 如下所示:
export const environment = {
...
togglesUrl: window.location.origin + '/release-toggling',
...
};

在我的 App 的 app.module 中,我将其配置如下:

...

const releaseToggleServiceConfig: ToggleServiceConfig = {
togglesUrl: environment.togglesUrl
};

...

@NgModule({
declarations: [AppComponent],
imports: [
CoreModule,
SharedModule,
...
ToggleModule.forRoot(releaseToggleServiceConfig)
],
...
})
export class AppModule {}

如果我硬编码window.location.origin(本地,到'localhost:4200'),那么它工作正常。显然,现阶段还不存在窗口。这可能与 aot 有关,但不幸的是,禁用 aot 不是一种选择。
正确传递 window.location.origin 的最佳模式/实践是什么。从技术上讲,在应用程序调用我的外部服务之前不需要它。
我对您的解决方案或建议感兴趣。这可能是很容易解决的问题,许多人以前都面临过。

最佳答案

All apps are served via a gateway and live on the same window.location.origin.


如果两个应用程序都存在于同一个网关上并且域也相同,那么也许您可以跳过 environment.ts 中的 window.location.origin:
export const environment = {
...
togglesUrl: '/release-toggling',
...
};

关于angular - Angular 模块中的 window.location.origin 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66639547/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com