zoukankan      html  css  js  c++  java
  • [Angular 2] Simple intro Http

    To use http, we need to import the HTTP_PROVIDER, so that we can inject http to other component:

    import {HTTP_PROVIDERS} from 'angular2/http';
    
    bootstrap(App, [
        HTTP_PROVIDERS
    ]);

    simple-request.ts:

    import {Component} from 'angular2/core';
    import {Http, Response} from 'angular2/http';
    @Component({
        selector: 'simple-request',
        template: `
            <button type="button" (click)="makeRequest()">Make Request</button>
            <div *ngIf="loading">loading...</div>
            <pre>{{data | json}}</pre>
        `
    })
    
    export class SimpleRequest{
        loading: boolean = false;
        data: Object;
        constructor(public http: Http){
    
        }
    
        makeRequest(){
            this.loading = true;
            this.http.request('https://api.github.com/users/zhentian-wan')
                .subscribe( (res: Response) => {
                    this.data = res.json();
                    this.loading = false;
                })
        }
    }
  • 相关阅读:
    Vue
    linux-----docker
    linux基础
    Flask基础
    websocket
    css
    Mysql数据库基础
    IO多路复用
    线程和协程
    sh_02_del关键字
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5318023.html
Copyright © 2011-2022 走看看