zoukankan      html  css  js  c++  java
  • angular的请求数据

    关于angular的数据请求的方式

    get请求:

    1.在app.module.js中引入相关的模块并声明

    import {HttpClientModule} from '@angualr/common/http';  //引入模块

    imports:[  //声明

      HttpClientModule

    ]

    2.在组件中引入并声明

    import {HttpClient} from '@angular/common/http'  //引入

    constructor(private http: HttpClient){  }  //声明

    3.调用函数

    ngOnInit(){

      let url = '请求的路径'

      //subsctibe: 利用rxjs封装的http获取一部请求的数据,类似于promise

      this.http.get(url).subscribe((data:any)=>{

        //data:返回的数据

      })

    }

    post请求:

    1.在app.module.js中引入相关的模块并声明

    import { HttpClientModule } from '@angualr/common/http';  //引入模块

    imports:[  //声明

      HttpClientModule

    ]

    2.在组件中引入并声明

    import {HttpClient,HttpHeaders,HttpParams} from '@angular/common/http'  //引入

    constructor(private http: HttpClient){  }  //声明

    3.调用函数

    ngOnInit(){

      //设置请求头

      从上图httpOptions = {headers:new HttpHeaders({'Content-Type':'application/json'})}

      let url = '请求的路径'

      //subsctibe: 利用rxjs封装的http获取一部请求的数据,类似于promise

      this.http.jsonp(url,'回调函数名称callback或cb').subscribe((data:any)=>{

        //data:返回的数据

      })

    }

    jsonp跨越请求

    1.在app.module.js中引入相关的模块并声明

    import {HttpClientModule,HttpClientJsonpModule} from '@angualr/common/http';  //引入模块

    imports:[  //声明

      HttpClientModule,

      HttpClientJsonpModule

    ]

    2.在组件中引入并声明

    import {HttpClient} from '@angular/common/http'  //引入

    constructor(private http: HttpClient){  }  //声明

    3.调用函数

    ngOnInit(){

      let url = '请求的路径'

      //subsctibe: 利用rxjs封装的http获取一部请求的数据,类似于promise

      this.http.get(url).subscribe((data:any)=>{

        //data:返回的数据

      })

    }

  • 相关阅读:
    如何用Baas快速在腾讯云上开发小程序-系列2:搭建Phabricator开发管理平台
    如何用Baas快速在腾讯云上开发小程序-系列1:搭建API & WEB WebSocket 服务器
    腾讯云极速配置NodeJS+LNMP运行环境
    Python GTK + 3教程 学习笔记 ——(4)Widget 小部件
    Python GTK + 3教程 学习笔记 ——(3)基础
    Python GTK + 3教程 学习笔记 ——(2)入门
    Python GTK + 3教程 学习笔记——(1)安装
    打包工具pyinstaller
    pip换源
    mac brew
  • 原文地址:https://www.cnblogs.com/violinh/p/12133828.html
Copyright © 2011-2022 走看看