Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 to 4 upgrade, moving from Http to HttpClient

I have a large application on Angular2 which is now ported to Angular4. However, it still uses the Http service.

I realize that Angular4 has HttpClient which is said to be better than Http service. However, I am but apprehensive about changing to HttpClient since it is used in many places and corresponding unit tests.

I would like to know, what is the best way to move from Http to HttpClient? What things should I consider from code and unit tests point of view?

Another reason I would like to move to HttpClient is to use the interceptor? Is it necessary to change to HttpClient for this?

Please advise. Many thanks in advance.

like image 535
Abhi Avatar asked Aug 23 '17 12:08

Abhi


1 Answers

Just my experience.

As I remember my biggest problem was to remove map in each http call.

Otherwise you just need to import HttpClientModule and inject HttpClient service instead of Http.

Also, there is some changes in unit testing. So if you have unit tests you have a bit more work. With HttpClient unit testing became much easier. Angular has great docs there, so I don't think it will be big problem. At least for me I did not have any problems with understanding this new stuff.

You can check this article for more information: http://brianflove.com/2017/07/21/migrating-to-http-client/.

like image 100
Sharikov Vladislav Avatar answered Oct 24 '22 09:10

Sharikov Vladislav