Streams give you the power to handle a "pending" state where you've made a request for data, but the data hasn't yet returned. You can leverage this pending stream to update your template as well which is often displayed as a "loader" or disabling some part of the UI.

<template>
<section class="section">
<button class="button" :disabled="disabled$" v-stream:click="click$">{{buttonText$}}</button>
<h2>
{{name$}}
</h2>
<img v-stream:error="imageError$" :src="data:image$" alt="">
</section>
</template> <script>
import { from, of, merge, throwError } from 'rxjs';
import {
switchMap,
pluck,
map,
mapTo,
catchError,
shareReplay,
share,
startWith
} from 'rxjs/operators'; export default {
name: 'app',
domStreams: ['click$', 'imageError$'],
subscriptions() {
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(() => of({name: 'Failed.. :('})),
share()
); const name$ = luke$.pipe(pluck('name')); const loadImage$ = luke$.pipe(
pluck('image'),
map(src => `https://starwars.egghead.training/${src}`)
); const failImage$ = this.imageError$.pipe(
mapTo(`http://via.placeholder.com/400x400`)
); const image$ = merge(
loadImage$,
failImage$
) const disabled$ = merge(
this.click$.pipe(mapTo(true)),
luke$.pipe(mapTo(false)),
).pipe(startWith(false)); const buttonText$ = disabled$.pipe(map(b => (b ? 'Loading...' : 'Load'))); return {
name$,
image$,
failImage$,
disabled$,
buttonText$
};
}
};
</script>

[Vue-rx] Disable Buttons While Data is Loading with RxJS and Vue.js的更多相关文章

  1. [RxJS] Reactive Programming - Clear data while loading with RxJS startWith()

    In currently implemention, there is one problem, when the page load and click refresh button, the us ...

  2. Vue 在beaforeCreate时获取data中的数据

    众所周知,vue在beforecreate时期是获取不到data中的 数据的 但是通过一些方法可以实现在beforecreate时获取到data中的数据 暂时想到两种放发可以实现,vue在before ...

  3. Vue父子组件通信(父级向子级传递数据、子级向父级传递数据、Vue父子组件存储到data数据的访问)

    Vue父子组件通信(父级向子级传递数据.子级向父级传递数据.Vue父子组件存储到data数据的访问) 一.父级向子级传递数据[Prop]: ● Prop:子组件在自身标签上,使用自定义的属性来接收外界 ...

  4. Vue学习之--------el与data的两种写法、MVVM模型、数据代理(2022/7/5)

    文章目录 1.el与data的两种写法 1.1.基础知识 1.2.代码实例 1.3.页面效果 2.MVVM模型 2.1. 基础知识 2.2 .代码实例 2.3.页面效果 3.数据代理 3.1. 基础知 ...

  5. VUE系列一:VUE入门:搭建脚手架CLI(新建自己的一个VUE项目)

    一.VUE脚手架介绍 官方说明:Vue 提供了一个官方的 CLI,为单页面应用快速搭建 (SPA) 繁杂的脚手架.它为现代前端工作流提供了 batteries-included 的构建设置.只需要几分 ...

  6. (vue.js)vue中引用了别的组件 ,如何使this指向Vue对象

    Vue中引用了别的组件 ,如何使this指向Vue对象 今天学习Vue组件传值, 通过创建Vue实例, 广播和监听实现传值, 但是传值之后无法直接将得到的值应用到Vue对象, 因为这相当于引用改了别的 ...

  7. 小白学习VUE第一篇文章---如何看懂网上搜索到的VUE代码或文章---使用VUE的三种模式:

    小白学习VUE第一篇文章---如何看懂网上搜索到的VUE代码或文章---使用VUE的三种模式: 直接引用VUE; 将vue.js下载到本地后本目录下使用; 安装Node环境下使用; ant-desig ...

  8. 循序渐进VUE+Element 前端应用开发(19)--- 后端查询接口和Vue前端的整合

    循序渐进VUE+Element 前端应用开发的系列文章中,前面介绍了系统各个功能的处理实现,本篇随笔从一个主线上介绍前后端开发的整合,让我们从ABP框架后端的查询接口的处理,前端API接口调用的封装, ...

  9. loading加载和layer.js

    layer.js中的loading加载 l本篇主要介绍layerjs中的loading加载在实际项目中的应用 1.使用的技术 前端:HTML5+CSS3+JS+layer.js 后端:.net 2.遇 ...

随机推荐

  1. EasyUI系列学习(十一)-Accordion(分类)

    一.加载 1.class加载 <div class="easyui-accordion" style="width:300px;height:200px" ...

  2. reduce的特殊用法

    //计算数组中每个元素出现的次数var arr = ["apple","orange","apple","orange" ...

  3. 什么 是JUnit?

    JUnit详解: http://www.cnblogs.com/eggbucket/archive/2012/02/02/2335697.html

  4. HTML5——移动端的点击、拖拽

    移动端浏览器不支持mouse事件 https://www.cnblogs.com/joyco773/p/6519668.html https://www.cnblogs.com/yjhua/p/525 ...

  5. window下编写python脚本在linux下运行出错 usr/bin/python^M: bad interpreter: No such file or directory

    今天在windows下使用notepad++写了个python脚本,传到linux服务器执行后提示:-bash: ./logger.py: usr/bin/python^M: bad interpre ...

  6. UIPageViewController 翻页、新手引导--UIScrollView:pagingEnabled

    UIPageViewController 翻页.新手引导--UIScrollView:pagingEnabled

  7. 安卓app测试之内存监控

    一.通过Dumpsys 来取值 1.adb shell dumpsys meminfo 获取的所有进程的内存信息,以及总内存,剩余内存,使用的内存等信息. 2.想获得某一进程内存的详细信息,在后面加上 ...

  8. iOS-关于一些手势冲突问题(scrollView 嵌套 tableView)

    简单说下关于开发中容易遇到的父试图添加手势与子试图点击事件冲突,UIScrollView 嵌套 UIScrollView . UIScrollView 嵌套 UITableView的情况手势冲突问题: ...

  9. 面向对象程序设计--Java语言第三周编程题:查找里程

    查找里程 题目内容: 下图为国内主要城市之间的公路里程: 你的程序要读入这样的一张表,然后,根据输入的两个城市的名称,给出这两个城市之间的里程. 注意:任何两个城市之间的里程都已经给出,不需要计算经第 ...

  10. 关押罪犯 - 并查集&优先队列

    题目地址:http://www.51cpc.com/web/problem.php?id=4261 Summarize: 此题最巧妙的是“敌人的敌人就是朋友!”,故需先将敌对关系放入优先队列,按怨恨值 ...