[Vue-rx] Disable Buttons While Data is Loading with RxJS and Vue.js
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的更多相关文章
- [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 ...
- Vue 在beaforeCreate时获取data中的数据
众所周知,vue在beforecreate时期是获取不到data中的 数据的 但是通过一些方法可以实现在beforecreate时获取到data中的数据 暂时想到两种放发可以实现,vue在before ...
- Vue父子组件通信(父级向子级传递数据、子级向父级传递数据、Vue父子组件存储到data数据的访问)
Vue父子组件通信(父级向子级传递数据.子级向父级传递数据.Vue父子组件存储到data数据的访问) 一.父级向子级传递数据[Prop]: ● Prop:子组件在自身标签上,使用自定义的属性来接收外界 ...
- 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. 基础知 ...
- VUE系列一:VUE入门:搭建脚手架CLI(新建自己的一个VUE项目)
一.VUE脚手架介绍 官方说明:Vue 提供了一个官方的 CLI,为单页面应用快速搭建 (SPA) 繁杂的脚手架.它为现代前端工作流提供了 batteries-included 的构建设置.只需要几分 ...
- (vue.js)vue中引用了别的组件 ,如何使this指向Vue对象
Vue中引用了别的组件 ,如何使this指向Vue对象 今天学习Vue组件传值, 通过创建Vue实例, 广播和监听实现传值, 但是传值之后无法直接将得到的值应用到Vue对象, 因为这相当于引用改了别的 ...
- 小白学习VUE第一篇文章---如何看懂网上搜索到的VUE代码或文章---使用VUE的三种模式:
小白学习VUE第一篇文章---如何看懂网上搜索到的VUE代码或文章---使用VUE的三种模式: 直接引用VUE; 将vue.js下载到本地后本目录下使用; 安装Node环境下使用; ant-desig ...
- 循序渐进VUE+Element 前端应用开发(19)--- 后端查询接口和Vue前端的整合
循序渐进VUE+Element 前端应用开发的系列文章中,前面介绍了系统各个功能的处理实现,本篇随笔从一个主线上介绍前后端开发的整合,让我们从ABP框架后端的查询接口的处理,前端API接口调用的封装, ...
- loading加载和layer.js
layer.js中的loading加载 l本篇主要介绍layerjs中的loading加载在实际项目中的应用 1.使用的技术 前端:HTML5+CSS3+JS+layer.js 后端:.net 2.遇 ...
随机推荐
- elastic-job 的简单使用
说明:这个是使用2.1.5版本 elastic-job是当当开源的的的定时任务,使用也是很简单的,可以解决数据量的大的时候可以分片执行,多应用节点部署时候不会重复执行. 是通过zookeeper作为控 ...
- poj2240 Arbitrage
思路: 有向图判负环. 实现: (1)spfa #include <iostream> #include <map> #include <string> #incl ...
- 图片选择器ImageEditContainer
1. 简介 本次demo中一共封装了两个组件:ImageEditButton 和 ImageEditContainer.其中ImageEditContainer 是在 ImageEditButton, ...
- javascript按钮点击事件问题
今天弄了个小测试,结果发现了点问题. 就是有一个按钮的点击事件,页面加载时候没反应,只有F12启用开发人员工具的时候才有反应.后来反复测试发现名字起的不太合理 function onclick(){ ...
- 关于.Net的强名称(Strong Name)
下面是我在CSDN上发表的<关于.Net的强名称(Strong Name)>,转载于此. 关于.Net的强名称(Strong Name)
- h5移动端混编总结
1.通信机制:解决是否能通信的问题: 2.接口:解决调用会话问题: 3.数据.URL正确性:解决数据.URL跳转路径正确性问题.
- R函数详解
字符串连接函数paste 1.字符串连接:paste(..., sep = " ", collapse = NULL)sep表示分隔符,默认为空格.collapse表示如果不指定值 ...
- Scrapy框架 之某网站产品采集案例
一.创建项目 第一步:scrapy startproject boyuan 第二步:cd boyuan scrapy genspider product -t crawl boyuan.com 如图 ...
- 梦想CAD控件网页版标注样式
增加标注样式 _DMxDrawX::AddDimStyle 增加一个新的标注样式,如果当前已经有指定名的标注样式,就直接失败返回.详细说明如下: 参数 说明 BSTR pszName 新增加的标注样式 ...
- 梦想CAD控件 2018.10.15更新
下载地址: http://www.mxdraw.com/ndetail_10105.html 1. 完善com接口的ToCurves函数,转换CAD文字,多行文字到曲线 2. 修改DrawImage接 ...