Abhi
Abhi

Reputation: 1671

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.

Upvotes: 6

Views: 992

Answers (1)

Sharikov Vladislav
Sharikov Vladislav

Reputation: 7269

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/.

Upvotes: 4

Related Questions