Angular学习之旅----angular6数据请求rxjs(rxjs6)

博客介绍在需要请求数据处引入Http模块、Jsonp模块及rxjs。RxJS用于异步数据流编程,Angular引入它让异步更可控简单。大部分RxJS操作符不在Angular的Observable基本实现中,需导入库扩展。还提及Angular6里RxJS使用方法有变化,如GET和JSONP请求写法改变,并附上完整代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要用到请求数据的地方引入 Http 模块 Jsonp 模块,以及 rxjs 
 
RxJS 是一种针对异步数据流编程工具,或者叫响应式扩展编程;可不管如何解释 RxJS 其
目标就是异步编程,Angular 引入 RxJS 为了就是让异步可控、更简单。 
 
大部分 RxJS 操作符都不包括在 Angular的 Observable 基本实现中,基本实现只包括
Angular 本身所需的功能。 
如果想要更多的 RxJS 功能,我们必须导入其所定义的库来扩展 Observable 对象, 以下
是这个模块所需导入的所有 RxJS 操作符:

而在angular6中,rxjs使用方法有所变化,下面是我得使用经验

// 使用rxjs
import { Observable } from 'rxjs'
// import 'rxjs/RX'
import { map } from 'rxjs/operators';
  // 注入依赖服务
  // 构造函数内部声明,私有化,实例化
  constructor(private http: Http, private jsonp: Jsonp) {

 GET请求写法有所变化

  requestData() {
    // alert('请求数据')
    var that = this;
    var url = "http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1";
    // this.http.get(url).subscribe(function (data) {
    // this.http.get(url).map(res=>res.json())    将返回的数据转换成json
    this.http.get(url).pipe(map(res => res)).subscribe(function (data) {
      // console.log(JSON.parse(data['_body']))
      var list = JSON.parse(data['_body'])
      that.list5 = list['result']
      console.log(that.list5)
    }, function (err) {
      console.log(err)
    })
  }

 

 JSONP请求方式也有所变化

  requestJsonpData() {
    // alert('请求数据')
    var that = this;
    var url = "http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1&callback=JSONP_CALLBACK";
    // this.jsonp.get(url).subscribe(function (data) {
    this.jsonp.get(url).pipe(map(res => res)).subscribe(function (data) {
      // console.log(JSON.parse(data['_body']))
      console.log(data['_body'])
      // 此处不需要进行字符串解析
      var list = data['_body']
      that.list5 = list['result']
      console.log(that.list5)
    }, function (err) {
      console.log(err)
    })
  }

附上完整的代码

import { Component, OnInit } from '@angular/core';
import { Http, Jsonp, Headers } from '@angular/http';
// 使用rxjs
import { Observable } from 'rxjs'
// import 'rxjs/RX'
import { map } from 'rxjs/operators';
@Component({
  selector: 'app-news',
  templateUrl: './news.component.html',
  styleUrls: ['./news.component.css']
})
export class NewsComponent implements OnInit {
  title = '你还news' // 定义属性
  msg: any // 另一种定义属性的方法
  msg1: string = '这是string类型的数据'
  // 定义属性还可以加修饰符
  public username = '张三'
  public h = ''
  public list = []
  public obj = {
    name: '张三'
  }
  public list2 = []
  public list4: any[]
  public list5: any[]
  private headers = new Headers({ 'Content-Type': 'application/json' })
  /**
   * public 共有 * 默认 可以在这个类里面使用,也可以在类外面使用
   * protected 保护类型  他只有在当前类和他的子类里面可以访问
   * private 私有 只有在当前类型才可以访问这个属性
   */
  // 注入依赖服务
  // 构造函数内部声明,私有化,实例化
  constructor(private http: Http, private jsonp: Jsonp) {
    this.h = '<h2>后台数据</h2>'
    this.list = ['1', '2', '3']
    this.list2 = [
      { 'title': '111' },
      { 'title': '222' },
      { 'title': '333' }
    ]
    this.list4 = [

      {
        'catename': "宝马",

        "list": [

          { 'title': '宝马x1' },
          { 'title': '宝马x3' },
          { 'title': '宝马x2' },
          { 'title': '宝马x4' },
        ]

      }, {
        'catename': "奥迪",

        "list": [

          { 'title': '奥迪q1' },
          { 'title': '奥迪q2' },
          { 'title': '奥迪q3' },
          { 'title': '奥迪q4' },
        ]

      },
    ]
  }

  ngOnInit() {
  }
  requestData() {
    // alert('请求数据')
    var that = this;
    var url = "http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1";
    // this.http.get(url).subscribe(function (data) {
    // this.http.get(url).map(res=>res.json())    将返回的数据转换成json
    this.http.get(url).pipe(map(res => res)).subscribe(function (data) {
      // console.log(JSON.parse(data['_body']))
      var list = JSON.parse(data['_body'])
      that.list5 = list['result']
      console.log(that.list5)
    }, function (err) {
      console.log(err)
    })
  }
  requestJsonpData() {
    // alert('请求数据')
    var that = this;
    var url = "http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1&callback=JSONP_CALLBACK";
    // this.jsonp.get(url).subscribe(function (data) {
    this.jsonp.get(url).pipe(map(res => res)).subscribe(function (data) {
      // console.log(JSON.parse(data['_body']))
      console.log(data['_body'])
      // 此处不需要进行字符串解析
      var list = data['_body']
      that.list5 = list['result']
      console.log(that.list5)
    }, function (err) {
      console.log(err)
    })
  }
  postData() {
    // 1.import { Http,Jsonp,Headers } from '@angular/http';   Headers 定义请求头的
    //2.private headers = new Headers({'Content-Type': 'application/json'});
    //3.post提交数据
    var url = "http://127.0.0.1:3000/dologin";
    this.http.post(url,
      JSON.stringify({ "username": 'zhangsan', "age": '20' }),
      { headers: this.headers }).subscribe(function (data) {
        console.log(data);
      }, function (error) {
        console.log(error);
      })
  }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑞朋哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值