Angular CLI: Change REST API URL on build

后端 未结 7 1672
心在旅途
心在旅途 2021-02-02 08:58

I want to remove my local server prefix from my REST API URLs (example, http://localhost:8080) when building for production (ng build --prod).

I get that i

7条回答
  •  醉酒成梦
    2021-02-02 09:22

    One possible way to achieve that is, to define different base URLs based on isDevMode() in your code. For example,

    import { isDevMode } from '@angular/core';
    
    // ...
    let baseUrl: string;
    if (isDevMode()) {
        baseUrl = "http://localhost:8080/";
    } else {
        baseUrl = "http://api.myprodserver.com/";
    }
    // ...
    

    Edit: This is meant to be for illustration. You'll likely want to use some type of (env-dependent) "config" in real code.

提交回复
热议问题