How to use/import http module?

前端 未结 13 2267
鱼传尺愫
鱼传尺愫 2020-11-27 16:40

I\'ve been playing with Angular 2 Quickstart.

How can I use/import http module in Angular 2?

I\'ve looked at Angular 2 Todo\'s.js, but it doesn\'t use the ht

13条回答
  •  温柔的废话
    2020-11-27 17:19

    Its already in angular2, so you dont need to put anything in package.json

    You just have to import and inject it like this. (this is a Stuff service with a methodThatUsesHttp() that just logs the response)

    import {XHR} from 'angular2/src/core/compiler/xhr/xhr';
    
    export class Stuff {
        $http;
        constructor($http: XHR) {
            this.$http = $http;
        }
    
        methodThatUsesHttp() {
            var url = 'http://www.json-generator.com/api/json/get/cfgqzSXcVu?indent=2';
    
            this.$http.get(url).then(function(res) {
                console.log(res);
            }, function(err) {
                console.log(err);
            });
        }
    }
    

提交回复
热议问题