1

I want to use static HTTP client service for some reasons. I tried it like the following but I got an error. Is creating static the service possible?

static service:

import { Http, URLSearchParams, Headers }  from '@angular/http';

export class ExampleService {
    static postSendTransaction(data, callback) {
        Http.post('https://xxxxxxxx', JSON.stringify(data), { // I got error here
            headers: new Headers({ 'Content-Type': 'application/json' })
        }).subscribe(
            response => {
                callback({data: response.json(), error: null});
            },
            error => {
                callback({data: null, error: error.statusText});
            }
        );
    }
}

example call:

ExampleService.postSendTransaction(this.data, postSendTransactionCallback);

I got this error:

Property 'post' does not exist on type 'typeof Http'.

3
  • are you sure you injected your service in the constructor? Commented Oct 8, 2017 at 20:36
  • Put simply, the Angular Http class is not designed to be used as a static object. You need to inject it into the constructor and use it the way it was designed. Commented Oct 8, 2017 at 22:06
  • You likely have XY problem, which means that the question should be modified to reflect the thing you're trying to do instead of the way you're trying to achieve it (which is wrong). Commented Oct 8, 2017 at 22:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.