java - 如何在 Spring Boot 中检索 url 并在 Angular 中使用它?
问题描述
在 Spring Boot 中,基本 url 可能localhost:8080
或不同的。现在我想以角度使用它。我在网上看到很多示例只是用 Angular http 客户端调用代码对其进行硬编码。
但是基本 url 可以更改,例如端口号更改或环境从 dev 更改为 qa 或 production。所以我的问题是可能从弹簧启动动态暴露它并从角度检索它?
解决方案
Your app should contain files at src/environments called environment.ts and environment.prod.ts. You can include a constant key value pair for your apiURL in this file like so:
export const environment = {
production: false,
apiUrl: 'localhost:8080'
}
And also include your production URL in the prod file. You can then use it in a file like so:
import { environment } from 'src/environment/environment.ts';
...
export class AppComponent implements OnInit {
url: string;
ngOnInit(): void {
this.url = environment.apiUrl;
}
}
推荐阅读
- python - 通过 Pycharm 找到 CodingBat Sum67 挑战解决方案,但无法在现场工作
- javascript - 在反应中转换和居中卡片
- ios - 使用 SKSprit 在 GameScene 和 GameViewController 之间调用函数
- image - 如何在颤动中将图像从资产保存到内部存储?
- android - 默认显示的吸气剂:显示!已弃用。在 Java 中已弃用
- android - 到达最后一个末尾时如何修改recycleviewer的项目数?
- angular - 有没有办法对所有消息只使用一个 p-toast?
- oracle - 在没有时间戳或索引的情况下跟踪 Oracle 9 上的数据更改
- javascript - 从单个 JSON 文件加载降价博客文章
- sas - 在 SAS EG 中将文件从一个目录复制到另一个目录不起作用