I have auto-generated a WebAPI service module with NSwagStudio to connect my Ionic/Angular app and my webservice together. This service (WebApiConnector.Client) is configured to be injected:
imports: [
BrowserModule,
IonicModule.forRoot(),
AppRoutingModule,
AngularFireModule.initializeApp(firebaseConfig),
AngularFireAuthModule,
HttpClientModule
],
providers: [
GooglePlus,
StatusBar,
SplashScreen,
{ provide: WebApiConnector.API_BASE_URL, useValue: environment.apiURL },
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy },
{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptorService, multi: true },
{ provide: ErrorHandler, useClass: GlobalErrorHandler},
AuthService,
WebApiConnector.Client
],
When I run the app on browser or debug it on android device, all works like a charm, but when I build it in production with:
ionic cordova build android --prod --release
the HttpClient injected into WebAPI service seems to be undefined because when the first call is performed, the app falls into this exception:
Uncaught (in promise): TypeError: Cannot read property 'request' of undefined
The call is performed inside ngAfterViewInit() hook.
I also tried to inject HttpClient into the page where "WebApiConnector.Client" is injected to perform manually the request to the server, but it perfectly worked.
This is the service module configuration
import { HttpClient, HttpHeaders, HttpResponse, HttpResponseBase } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class Client {
private http: HttpClient;
private baseUrl: string;
protected jsonParseReviver: ((key: string, value: any) => any) | undefined = undefined;
constructor(@Inject(HttpClient) http: HttpClient, @Optional() @Inject(API_BASE_URL) baseUrl?: string) {
this.http = http;
this.baseUrl = baseUrl ? baseUrl : "/webapi_2";
}
and this is the snippet of code that throw the exception:
return this.http.request("get", url_, options_)
Where is the problem?