Using the new HttpClient to deal with XHR requests
The vast majority of web applications nowadays use XMLHttpRequest
(XHR) requests, and applications made with Angular are no different. For that, we have the HTTPClient
module that replaced the old HTTP module in previous versions.
In this session, we will understand how to use XHR requests inside our Angular services.
It is highly recommended that you use Angular services to handle requests of this type so that the code of the component is more organized and easy to maintain.
Note
You can read more about XHR requests at https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest.
Creating the auth service
Let's create the file that will store the necessary code for our authentication module:
- Still in your Terminal, type the following command:
ng g service pages/auth/_services/auth
The previous command will create a new folder and file in ./app/pages/auth/_services/auth.service.ts
. Now, let's add some pieces of code.
- Open
./app/pages/auth/_services...