【问题标题】:How to Get Dynamic url from spring to angular如何获取从弹簧到角度的动态网址
【发布时间】:2019-04-19 11:23:04
【问题描述】:

我的后端是 spring boot,前端是 Angular 7。我希望 spring-boot 本地服务器地址在路由时在 Angular 中动态获取。 因此,如果 Spring 引导服务器中的端口地址发生更改,它也应该反映在 Angular 中吗? 任何想法如何做到这一点。

我尝试使用 window.location.host 但它反映了 Angular 的服务器地址(即 localhost//4200 )而不是 (localhost//8080)。

_url ='http://localhost:8080/home/delete/';
_updateurl ='http://localhost:8080/home/delete/';

相反,它应该显示为

url=SpringBoot+'home/delete'

【问题讨论】:

    标签: angular spring-boot url angular7


    【解决方案1】:

    您应该使用 Angular ActivatedRoute。它将返回您的 Angular 应用程序所在的相对 url。它不会考虑您部署此应用的位置。

    您可以观察您当前的网址:

    this.activatedRoute.url.subscribe(url => {
        // Do something with your current url when it changes
    });
    

    或者只是获取网址的即时快照:

    const snapShot = this.activatedRoute.snapshot;
    

    【讨论】:

    【解决方案2】:

    所以人们我得到了我的问题的解决方案。到目前为止,我还没有找到一种方法可以让 Angular 扫描后端端口的变化并相应地进行更改,但我们可以做的是使用 Angular 中的 Environment.ts 所以基本上我们只需要放

    export const environment = {  
     Url: 'http://localhost:8080',
    debugMode: false
    };
    

    只要你需要它,只需导入环境并添加 console.log(environment.Url+'abc');

    它会起作用的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 2019-02-27
      • 2018-06-12
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 2018-08-09
      • 2019-05-17
      相关资源
      最近更新 更多