分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

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

发布时间:2023-09-06 01:51责任编辑:沈小雨关键词:jsAngular

一 of操作符

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); ?????} ???); ?}}

二 from操作符

import { Component, OnInit } from ‘@angular/core‘;import { from } from ‘rxjs/observable/from‘;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> = from(arr); ???colors.subscribe((color: string) => { ?????console.log(color); ???}); ?}}

三 interval操作符

返回从0开始的无限自增整数序列,每个固定的时间间隔发送。第一次并 没有立马去发送, 而是第一个时间段过后才发出。

import { Component, OnInit } from ‘@angular/core‘;import { interval } from ‘rxjs/observable/interval‘;import { Observable } from ‘rxjs/Observable‘;@Component({ ?selector: ‘app-create‘, ?templateUrl: ‘./create.component.html‘, ?styleUrls: [‘./create.component.css‘]})export class CreateComponent implements OnInit { ?constructor() { } ?ngOnInit() { ???interval(1000).subscribe((val: number) => { ?????console.log(val); ???}); ?}}

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

原文地址:https://www.cnblogs.com/sea-breeze/p/8968724.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved