Got error when user Http?

ForumGot error when user Http?
youxingzhi asked 6 months ago
Start your code here
import {Http} from '@angular/http';

@Component({
moduleId: module.id,
selector: 'test-cmp',
templateUrl: 'test.html',
styleUrls: ['test.css'],
})

export class TestComponent {
public mobiles;
constructor(public http:Http) {
this.http.get('test.json').subscribe(res=> this.mobiles =res.json());
}
}
The error is:
No provider for http
2 Answers
Best Answer
admin Staff answered 6 months ago

Hi youxingzhi,

Add HTTP_PROVIDERS in bootstrap method defined in main.ts file

youxingzhi answered 6 months ago

Yes, I did as you say:
import { HTTP_PROVIDERS } from ‘@angular/http';
bootstrap(AppComponent, [
ROUTER_PROVIDERS,
HTTP_PROVIDERS,
provide(APP_BASE_HREF, { useValue: ‘<%= APP_BASE %>’ })
]);
I have resolved it. Thank you!

admin Staff replied 6 months ago

That’s awesome!
You’re welcome. :)