how to inject API_BASE_URL (a string) in an angular service

后端 未结 3 1848
小鲜肉
小鲜肉 2021-01-01 21:31

this autogenerated service (by NSwagStudio) needs an API_BASE_URL (InjectionToken) value in order to perform http requests how and where i can inject it?

/*         


        
3条回答
  •  臣服心动
    2021-01-01 22:13

    On the parent module create a provider for API_BASE_URL

    export function getBaseUrl(): string {
      return AppConsts.baseUrl;
    }
    
    @NgModule({
      declarations: [AppComponent],
      imports: [BrowserModule],
      providers: [{ provide: API_BASE_URL, useFactory: getBaseUrl }],
       bootstrap: [AppComponent]
    })
    export class AppModule {}
    

    and then define a AppConsts class with static properties as such

    export class AppConsts {
      static baseUrl = "your_api_base_url"; 
    }
    

    Worked for me, hope it help. This solution is based on aspnet boilerpate angular project, which for me give the best standards on architecture. I leave you here the url for the angular project + the url for this specific code.

提交回复
热议问题