How to use/import http module?

前端 未结 13 2308
鱼传尺愫
鱼传尺愫 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:22

    import {Injectable} from 'angular2/core';
    import {Http, HTTP_PROVIDERS} from 'angular2/http';
    
    @Injectable()
    export class GroupSelfService {
        items:Array;
    
        constructor(http:Http){
            http.get('http://127.0.0.1:8080/src/data/names.json')
            .subscribe(res => {
                this.items = res;
                console.log('results found');
            })
        }
    }
    

    Results in a 404:
    File change detected
    File change detected
    GET /src/angular2/http 404 0.124 ms - 30

    Two strange things:
    1. /src/angular2/http - is not the path where http can be found and not the path I've provided in the code.
    2. core.js lies just beside http.js in the node_modules/angular2 folder and is found.

    How strange is that?

    Update Mea culpa: None of the examples mentioned that you need to reference the http.js in your html like

    ...and then it worked.
    But for the path in the error message I still have no explanation.

提交回复
热议问题