Web组件、多环境中的跨域API调用

发布于 2025-01-12 18:44:50 字数 1054 浏览 2 评论 0原文

我有两个问题:

1)

我正在 Angular 中开发一个 Web 组件。在我们的项目中,我们有多个测试环境+生产环境。假设域上有多个 API,如下所示:

https://domain.dev1.com/api/v1< /a>

https://domain.dev2.com/api/v1

https://domain.dev3.com/api/v1

https://domain.prod.com/api/v1

Web 组件必须在所有环境中运行。重要的事实是,该 Web 组件将在与 API 不同的域上工作。 (假设https://anotherdomain.com)。如何正确配置呢?我正在考虑使用这些 url 的 4 个不同的environment.ts 文件。比如:

file: environment.dev1.ts

export class Environment {
   apiUrl: https://domain.dev1.com/api/v1
}

并在代码中使用这个变量。这是正确的解决方案吗?

2)

在此 Web 组件中,我将使用另一个 Web 组件,该组件配置为在同一域上使用 API(API 的相对路径)。我可以以某种方式创建一个代理来使用这个 API 吗? (正如我之前所说,我的网络组件位于不同的域上)。

希望我能很好地解释一切。

I have 2 questions:

1)

I'm working on a web component in Angular. In our project, we have multiple test environments + production. Assume there are multiple APIs on domains as follow:

https://domain.dev1.com/api/v1

https://domain.dev2.com/api/v1

https://domain.dev3.com/api/v1

https://domain.prod.com/api/v1

Web-component must be working across all environments. Important fact is, that web-component will be working on DIFFERENT domain than API. (lets say https://anotherdomain.com). How to configure it properly? I was thinking of 4 different environment.ts files with these urls. Sth like:

file: environment.dev1.ts

export class Environment {
   apiUrl: https://domain.dev1.com/api/v1
}

and use this variable in code. Is that proper solution?

2)

In this web-component I will be using another web component, that is configured to use API on the same domain (relative paths to API). Can i make somehow a proxy to use this API? (as I said before, my web-component is on different domain).

Hope I explained everything well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文