Skip to content

Instantly share code, notes, and snippets.

@denisoster
Created March 9, 2018 11:13
Show Gist options
  • Save denisoster/3dd2dddec0cb422f2c9edea26773cf5e to your computer and use it in GitHub Desktop.
Save denisoster/3dd2dddec0cb422f2c9edea26773cf5e to your computer and use it in GitHub Desktop.
base.url Angular 5
import { Injectable } from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';
import { environment } from "../../../../environments/environment";
@Injectable()
export class APIHttpInterceptor implements HttpInterceptor {
private baseUrl: string = environment.API_BASE_URL;
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const apiReq = req.clone({ url: `${this.baseUrl}/${req.url}` });
return next.handle(apiReq);
}
}
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import {AppRoutingModule} from './app-routing.module';
import {AppComponent} from './app.component';
import {HttpClientModule, HTTP_INTERCEPTORS} from '@angular/common/http';
import {APIHttpInterceptor} from "./system/shared/interactors/APIhttp.interactor";
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule
],
providers: [{
provide: HTTP_INTERCEPTORS,
useClass: APIHttpInterceptor,
multi: true
}],
bootstrap: [AppComponent]
})
export class AppModule {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment