0
My code is like this, I would like to know how to send an Authorization token in all http requests made in Ionic 3:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Component({
selector: 'page-categoria',
templateUrl: 'categoria.html'
})
export class CategoriaPage {
constructor(public nav: NavController, public httpClient: HttpClient) { }
toTest() {
let data = {};
let url = 'https://localhost/gestor2.0/public/categoria/';
let headers = new HttpHeaders({
'Authorization': 'Bearer ' + 'TdSa4512csdgfa368747654'
});
this.httpClient.post(url, data, { headers: headers })
.subscribe((result: any) => {
console.log('sucesso ao salvar');
console.log(result);
},
(error) => {
console.log(error);
});
}
}
Returns this error when opening the category page:
core.js:1449 ERROR Error: Uncaught (in Promise): Error: Staticinjectorerror(Appmodule)[Categoriapage -> Httpclient]: Staticinjectorerror(Platform: core)[Categoriapage -> Httpclient]: Nullinjectorerror: No Provider for Httpclient! Error: Staticinjectorerror(Appmodule)[Categoriapage -> Httpclient]: Staticinjectorerror(Platform: core)[Categoriapage -> Httpclient]: Nullinjectorerror: No Provider for Httpclient! at Nullinjector.get (core.js:1003) resolveat Token (core.js:1301) at tryResolveToken (core.js:1243) At Staticinjector.get (core.js:1111) resolveat Token (core.js:1301) at tryResolveToken (core.js:1243) At Staticinjector.get (core.js:1111) resolveat NgModuleDep (core.js:10896) At Ngmoduleref.get (core.js:12129) resolveat Dep (core.js:12619) at c (polyfills.js:3) At Object.Reject (polyfills.js:3) At Tab.NavControllerBase. _fireError (Nav-controller-base.js:223) At Tab.NavControllerBase. _failed (Nav-controller-base.js:216) at Nav-controller-base.js:263 at t.invoke (polyfills.js:3) At Object.onInvoke (core.js:4760) at t.invoke (polyfills.js:3) at r.run (polyfills.js:3) at polyfills.js:3
Take a look: Implementing Interceptor for HTTP requests in your Angular 4 application
– Marconi