RxJS 核心操作符详细用法示例

1. Observable 详细用法

Observable 是 RxJS 的核心概念,代表一个可观察的数据流。

创建和订阅 Observable

import { Observable } from "rxjs";// 1. 创建Observable
const myObservable = new Observable(subscriber => {// 发出三个值subscriber.next('第一个值');subscriber.next('第二个值');subscriber.next('第三个值');// 模拟异步操作setTimeout(() => {subscriber.next('异步值');subscriber.complete(); // 完成流}, 1000);// 可选的清理逻辑return () => {console.log('Observable被取消订阅');};
});// 2. 订阅Observable
const subscription = myObservable.subscribe({next: value => console.log('收到值:', value),error: err => console.error('发生错误:', err),complete: () => console.log('流已完成')
});// 3. 取消订阅 (通常在组件销毁时调用)
setTimeout(() => {subscription.unsubscribe();
}, 2000);/* 输出顺序:
收到值: 第一个值
收到值: 第二个值
收到值: 第三个值
(等待1秒)
收到值: 异步值
流已完成
(再等待1秒)
Observable被取消订阅
*/

2. of 操作符详细用法

of 用于创建一个会立即发出给定参数的 Observable。

基本示例

import { of } from "rxjs";// 发出固定值
of('苹果', '香蕉', '橙子').subscribe({next: fruit => console.log('水果:', fruit),complete: () => console.log('水果列表结束')
});/* 输出:
水果: 苹果
水果: 香蕉
水果: 橙子
水果列表结束
*/// 发出不同类型的数据
of('字符串',123,true,{name: 'Alice'},[1, 2, 3],function hello() { return 'world'; }
).subscribe(val => console.log('收到的值:', val));// 实际应用:模拟API返回
function mockApiCall() {return of({id: 1, name: '用户1'});
}mockApiCall().subscribe(user => {console.log('用户数据:', user);
});

3. from 操作符详细用法

from 可以将多种数据类型转换为 Observable。

各种来源的转换

import { from } from "rxjs";// 1. 从数组创建
from([10, 20, 30]).subscribe(num => console.log('数字:', num));// 2. 从Promise创建
const promise = fetch('https://api.example.com/data').then(response => response.json());from(promise).subscribe(data => {console.log('API数据:', data);
});// 3. 从字符串创建 (每个字符作为单独的值)
from('Hello').subscribe(char => console.log(char));
// 输出: H, e, l, l, o// 4. 从Map或Set创建
const myMap = new Map();
myMap.set('name', 'Alice');
myMap.set('age', 25);from(myMap).subscribe(entry => {console.log('Map条目:', entry);// 输出: ['name', 'Alice'], ['age', 25]
});// 5. 实际应用:批量处理数组
const userIds = [1, 2, 3, 4];from(userIds).subscribe(id => {console.log('处理用户ID:', id);// 这里可以调用API获取每个用户的详细信息
});

4. forkJoin 操作符详细用法

forkJoin 用于并行执行多个 Observable,等待它们全部完成。

完整示例

import { forkJoin, of, from, throwError } from "rxjs";
import { delay, catchError } from "rxjs/operators";// 模拟API函数
function getUser(id) {return of({ id, name: `用户${id}` }).pipe(delay(1000));
}function getUserPosts(userId) {const posts = [{ id: 1, title: '帖子1' },{ id: 2, title: '帖子2' }];return of(posts).pipe(delay(1500));
}function getUserComments(userId) {return from(fetch(`https://api.example.com/users/${userId}/comments`));
}// 1. 基本用法
forkJoin([getUser(1),getUserPosts(1),getUserComments(1).pipe(catchError(error => of(`获取评论失败: ${error.message}`)))
]).subscribe({next: ([user, posts, comments]) => {console.log('用户:', user);console.log('帖子:', posts);console.log('评论:', comments);},error: err => console.error('整体失败:', err),complete: () => console.log('所有请求完成')
});// 2. 对象形式更清晰
forkJoin({user: getUser(1),posts: getUserPosts(1),comments: getUserComments(1).pipe(catchError(error => of([])) // 错误时返回空数组)
}).subscribe({next: result => {console.log('整合结果:', result);// 结构: { user: {...}, posts: [...], comments: [...] }}
});// 3. 错误处理演示
forkJoin({success: of('成功'),failure: throwError(new Error('出错了'))
}).pipe(catchError(error => {console.log('捕获到错误:', error);return of({ success: null, failure: error.message });})
).subscribe(result => {console.log('最终结果:', result);
});// 4. 实际应用:并行请求多个API
function loadDashboardData() {return forkJoin({user: getUser(1),notifications: from(fetch('/api/notifications')),settings: from(fetch('/api/settings'))});
}loadDashboardData().subscribe(data => {console.log('仪表盘数据:', data);// 更新UI...
});

综合实战示例

import { forkJoin, from, of } from "rxjs";
import { map, mergeMap, catchError } from "rxjs/operators";// 模拟API服务
class ApiService {static getUsers() {const users = [{ id: 1, name: 'Alice' },{ id: 2, name: 'Bob' }];return of(users).pipe(delay(500));}static getUserDetails(userId) {const details = {1: { age: 25, email: 'alice@example.com' },2: { age: 30, email: 'bob@example.com' }};return of(details[userId]).pipe(delay(300));}static getUserPosts(userId) {const posts = {1: [{ id: 101, title: 'Alice的第一篇帖子' }],2: [{ id: 201, title: 'Bob的帖子' }, { id: 202, title: 'Bob的另一篇帖子' }]};return of(posts[userId] || []).pipe(delay(700));}
}// 1. 获取所有用户及其详细信息和帖子
ApiService.getUsers().pipe(mergeMap(users => {// 为每个用户创建请求数组const userRequests = users.map(user => forkJoin({details: ApiService.getUserDetails(user.id),posts: ApiService.getUserPosts(user.id)}).pipe(map(data => ({ ...user, ...data }))));// 并行执行所有用户请求return forkJoin(userRequests);})
).subscribe({next: completeUsers => {console.log('完整用户数据:', completeUsers);/* 输出:[{id: 1,name: 'Alice',details: { age: 25, email: 'alice@example.com' },posts: [{ id: 101, title: 'Alice的第一篇帖子' }]},{id: 2,name: 'Bob',details: { age: 30, email: 'bob@example.com' },posts: [{...}, {...}]}]*/},error: err => console.error('获取用户数据失败:', err)
});// 2. 实际应用:表单提交后并行更新多个资源
function updateResources(userData, postsData, settingsData) {return forkJoin({user: from(fetch('/api/user', {method: 'PUT',body: JSON.stringify(userData)})),posts: from(fetch('/api/posts', {method: 'POST',body: JSON.stringify(postsData)})),settings: from(fetch('/api/settings', {method: 'PATCH',body: JSON.stringify(settingsData)}))}).pipe(map(responses => ({user: responses.user.json(),posts: responses.posts.json(),settings: responses.settings.json()})));
}// 使用示例
updateResources({ name: '新名字' },[{ title: '新帖子' }],{ theme: 'dark' }
).subscribe({next: results => {console.log('所有资源更新成功:', results);},error: err => {console.error('更新失败:', err);// 显示错误提示}
});

这些示例展示了 RxJS 操作符在实际开发中的典型用法。关键点:

  1. Observable 是基础,代表数据流
  2. of 用于创建简单的同步流
  3. from 用于从各种数据源创建流
  4. forkJoin 用于并行执行多个 Observable 并合并结果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/diannao/84238.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

QGrphicsScen画布网格和QGrphicsItem对齐到网格

#include <QGraphicsScene> #include <QPainter> #include <QWheelEvent> #include <QGraphicsView> class MyGraphicsView : public QGraphicsView { public:MyGraphicsView(QGraphicsScene* scene) : QGraphicsView(scene){}protected:// 重写滚轮事…

深入解析自然语言处理中的语言转换方法

在数字化浪潮席卷全球的今天&#xff0c;自然语言处理&#xff08;Natural Language Processing&#xff0c;NLP&#xff09;作为人工智能领域的核心技术之一&#xff0c;正深刻地改变着我们与机器交互的方式。其中&#xff0c;语言转换方法更是 NLP 的关键组成部分&#xff0c…

VRRP虚拟路由器协议的基本概述

目录 vrrp是什么&#xff1f; VRRP的一些概念与专有名词 VRRP的Master选举规则&#xff1a; 尾声 vrrp是什么&#xff1f; vrrp全名virtual router redundance protocol&#xff0c;虚拟路由器冗余协议 VRRP的一些概念与专有名词 1&#xff09;VRRP设备&#xff1a;运行VRRP…

数据结构 -- 交换排序(冒泡排序和快速排序)

冒泡排序 基于“交换”的排序&#xff1a;根据序列中两个元素关键字的比较结果来对换这两个记录在序列中的位置 //交换 void swap(int &a,int &b){int temp a;a b;b temp; }//冒泡排序 void BubbleSort(int A[],int n){for(int i0;i<n-1;i){bool flag false; …

多模态AI终极形态?GPT-5与Stable Diffusion 3的融合实验报告

多模态AI终极形态&#xff1f;GPT-5与Stable Diffusion 3的融合实验报告 系统化学习人工智能网站&#xff08;收藏&#xff09;&#xff1a;https://www.captainbed.cn/flu 文章目录 多模态AI终极形态&#xff1f;GPT-5与Stable Diffusion 3的融合实验报告摘要引言技术架构对…

ajax中get和post的区别,datatype返回的数据类型有哪些?

GET 请求 和 POST 请求 是 HTTP 协议中常用的两种请求方法&#xff0c;它们主要的区别在于&#xff1a; GET 请求&#xff1a; 数据传输方式&#xff1a;数据通过 URL 传递&#xff0c;通常是附加在 URL 后面的查询字符串中&#xff0c;例如 https://example.com/page?nameJoh…

101 alpha_59

(0 - (1 * (rank((sum(returns, 10) / sum(sum(returns, 2), 3))) * rank((returns * cap))))) 0 - (1 * A * B) A rank((sum(returns, 10) / sum(sum(returns, 2), 3)))B rank((returns * cap)) sum(returns, 10)&#xff1a;计算过去 10 期收益率的总和sum(returns, 2)&…

vscode里几种程序调试配置

标题调试python嵌入的c代码,例如 import torch from torch.utils.cpp_extension import loadtest_load load(nametest_load, sources[test.cpp],extra_cflags[-O0, -g],#extra_cflags[-O1],verboseTrue, ) a torch.tensor([1, 2, 3]) b torch.tensor([4, 5, 6]) result te…

深入解析MySQL中的HAVING关键字:从入门到实战

引言 在SQL查询中&#xff0c;数据过滤是核心操作之一。我们常用WHERE子句进行行级过滤&#xff0c;但当需要对分组后的结果进行条件筛选时&#xff0c;HAVING关键字便成为不可或缺的工具。本文将深入探讨HAVING的作用、使用场景及其与WHERE的区别&#xff0c;并通过实际案例帮…

根据YOLO数据集标签计算检测框内目标面积占比(YOLO7-10都适用)

程序&#xff1a; 路径改成自己的&#xff0c;阈值可以修改也可以默认 #zhouzhichao #25年5月17日 #计算时频图中信号面积占检测框面积的比值import os import numpy as np import pandas as pd from PIL import Image# Define the path to the directory containing the lab…

AI神经网络降噪 vs 传统单/双麦克风降噪的核心优势对比

1. 降噪原理的本质差异 对比维度传统单/双麦克风降噪AI神经网络降噪技术基础基于固定规则的信号处理&#xff08;如谱减法、维纳滤波&#xff09;基于深度学习的动态建模&#xff08;DNN/CNN/Transformer&#xff09;噪声样本依赖预设有限噪声类型训练数据覆盖数十万种真实环境…

了解Android studio 初学者零基础推荐(3)

kotlin中的数据类及对象 使用泛型创建可重复使用的类 我们将常在线答题考试&#xff0c;有的考试题型包括判断&#xff0c;或者填空&#xff0c;以及数学题&#xff0c;此外试题内容还包括难易程度&#xff1a;"easy”,"medium"&#xff0c;"hard",…

【占融数科-注册/登录安全分析报告】

前言 由于网站注册入口容易被黑客攻击&#xff0c;存在如下安全问题&#xff1a; 暴力破解密码&#xff0c;造成用户信息泄露短信盗刷的安全问题&#xff0c;影响业务及导致用户投诉带来经济损失&#xff0c;尤其是后付费客户&#xff0c;风险巨大&#xff0c;造成亏损无底洞…

记录一次请求数据很慢的灾难

起因&#xff1a; 因公司业务需要&#xff0c;对接了一个平台的 api。对接完成之后&#xff0c;发现只要打开开关&#xff0c;就别的接口就访问很慢&#xff0c;出现 gatway time out。 排查&#xff1a; 先看下主服务器和 slave 服务器的状态&#xff1a; 主服务&#xff…

力扣-将x减到0的最小操作数

1.题目描述 2.题目链接 1658. 将 x 减到 0 的最小操作数 - 力扣&#xff08;LeetCode&#xff09; 3.题目分析 1&#xff09;正面求解困难 题目要求我们每次都从最左边或者最右边取一个数&#xff0c;使x-元素的值&#xff0c;并在数组中移除该元素。最后返回的最小操作数…

排序复习/上(C语言版)

目录 1.排序概念 2.冒泡排序 效率性能测试代码&#xff1a; 性能分析&#xff1a; 3.直接插入排序 单趟&#xff1a; 整体&#xff1a; 性能分析&#xff1a; 4.希尔排序&#xff08;基于插入排序的优化&#xff09; 单趟单组&#xff1a; 单趟多组&#xff1a; 降低…

程序编辑器快捷键总结

程序编辑器快捷键总结 函数跳转 函数跳转 Creator : F2VSCode : F12visual Studio : F12

【LUT技术专题】极小尺寸LUT算法:TinyLUT

TinyLUT: Tiny Look-Up Table for Efficient Image Restoration at the Edge&#xff08;2024 NeurIPS&#xff09; 专题介绍一、研究背景二、TinyLUT方法2.1 Separable Mapping Strategy2.2 Dynamic Discretization Mechanism 三、实验结果四、总结 本文将从头开始对TinyLUT: …

解决:VMware 虚拟机 Ubuntu 系统共享文件夹无法访问问题

以下是解决 VMware 虚拟机 Ubuntu 系统共享文件夹无法访问 问题的完整过程总结&#xff0c;按关键步骤和逻辑顺序梳理&#xff1a; 系统版本&#xff1a;Ubuntu 22.04.5 1. 确认 VMware Tools 已安装 验证方法&#xff1a;通过 ps -ef | grep vmtoolsd 检查是否存在 vmtools…

YOLOv8 的双 Backbone 架构:解锁目标检测新性能

一、开篇&#xff1a;为何踏上双 Backbone 探索之路 在目标检测的领域中&#xff0c;YOLOv8 凭借其高效与精准脱颖而出&#xff0c;成为众多开发者和研究者的得力工具。然而&#xff0c;传统的单 Backbone 架构&#xff0c;尽管已经在诸多场景中表现出色&#xff0c;但仍存在一…