Rxjs之创建操作符(Angular环境)

一 of操作符

从一个数组、类数组对象、Promise、迭代器对象或者类 Observable 对象创建一个 Observable.

import { Component, OnInit } from '@angular/core';
import { of } from 'rxjs/observable/of';
import { Observable } from 'rxjs/Observable';

@Component({
  selector: 'app-create',
  templateUrl: './create.component.html',
  styleUrls: ['./create.component.css']
})
export class CreateComponent implements OnInit {

  constructor() { }

  ngOnInit() {

    // 从数组创建

    const arr = ['red', 'yellow', 'blue'];
    const colors: Observable<string[]> = of(arr);
    colors.subscribe((colorsArr: string[]) => {
      console.log(colorsArr);
    });

    // 从迭代器对象创建

    const map: Map<string, any> = new Map();
    map.set('fruit', 'orange');
    of(map).subscribe(
      (fruitsMap: Map<string, any>) => {
        console.log(fruitsMap);
      }
    );
  }

}

猜你喜欢

转载自www.cnblogs.com/sea-breeze/p/8968724.html