博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Vue-rx] Share RxJS Streams to Avoid Multiple Requests in Vue.js
阅读量:5338 次
发布时间:2019-06-15

本文共 1297 字,大约阅读时间需要 4 分钟。

Splitting a stream into multiple streams causes new subscriptions. You can think of new subscriptions as "invoking a function". So if your original stream makes a request for data, splitting the original stream will make 2 requests for data. The way to get around this is by using share so that each time the stream is split, all the split stream stay synced together.

 

For the RxJS Code below, it issues network reuqest twice:

const createLoader = url => from(this.$http.get(url)).pipe(pluck('data'));    const luke$ = this.click$.pipe(      mapTo('https://starwars.egghead.training/people/1'),      switchMap(createLoader),      catchError(() => createLoader('https://starwars.egghead.training/people/2'))    );    const name$ = luke$.pipe(pluck('name'));    const image$ = luke$.pipe(      pluck('image'),      map(src => `https://starwars.egghead.training/${src}`)    );

Because both 'name$' and 'image$' will trigger 'luke$', then 'createLoader'.

 

In order to solve the problem we can use 'share()' or 'shareReplay(1)':

const luke$ = this.click$.pipe(      mapTo('https://starwars.egghead.trainin/people/1'),      switchMap(createLoader),      catchError(() => createLoader('https://starwars.egghead.training/people/2')),      share()    );

 

转载于:https://www.cnblogs.com/Answer1215/p/9331046.html

你可能感兴趣的文章
关于python中带下划线的变量和函数 的意义
查看>>
linux清空日志文件内容 (转)
查看>>
安卓第十三天笔记-服务(Service)
查看>>
Servlet接收JSP参数乱码问题解决办法
查看>>
【bzoj5016】[Snoi2017]一个简单的询问 莫队算法
查看>>
Ajax : load()
查看>>
MySQL-EXPLAIN执行计划Extra解释
查看>>
Zookeeper概述
查看>>
Zookeeper一致性级别
查看>>
Linux远程登录
查看>>
Linux自己安装redis扩展
查看>>
HDU 1016 Prime Ring Problem(dfs)
查看>>
C#中结构体与字节流互相转换
查看>>
session和xsrf
查看>>
跟随大神实现简单的Vue框架
查看>>
Linux目录结构
查看>>
LeetCode-Strobogrammatic Number
查看>>
luoguP3414 SAC#1 - 组合数
查看>>
五一 DAY 4
查看>>
(转)接口测试用例设计(详细干货)
查看>>