[Vue-rx] Pass Template Data Through domStreams in Vue.js and RxJS
2023-09-14 09:00:50 时间
domStreams
enable you to pass additional data along the stream that can be provided by the template (such as data coming from a v-for
rendering of an Array). You can then pluck
off the data from the stream and use it further down the stream.
<template> <section class="section"> <button class="button" :disabled="disabled$" v-stream:click="{subject: click$, data: 1}">{{buttonText$}}</button> <h2> {{name$}} </h2> <img v-stream:error="imageError$" :src="image$" alt=""> </section> </template> <script> import { from, of, merge, throwError } from 'rxjs'; import { exhaustMap, 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( pluck('data'), map(id => `https://starwars.egghead.training/people/${id}`), exhaustMap(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>
相关文章
- 使用 Vuex + Vue.js 构建单页应用
- VUE - vue.runtime.esm.js?6e6d:619 [Vue warn]: Do not use built-in or reserved HTML elements as component i
- Vue computed属性
- Easy Mock以及Vue+Mock.js模拟数据
- [Vue] Lazy Load a Route by using the Dynamic Import in Vue.js
- [Vue] Conditionally Render DOM Elements in Vue.js (v-if v-else v-show)
- [Vue-rx] Switch to a Function which Creates Observables with Vue.js and Rxjs
- [Vue-rx] Handle Image Loading Errors in Vue.js with RxJS and domStreams
- [Vue] Lazy Load a Route by using the Dynamic Import in Vue.js
- [Vue + TS] Using Route events inside Vue
- [Nuxt] Add Arrays of Data to the Vuex Store and Display Them in Vue.js Templates
- [Vue] Conditionally Render DOM Elements in Vue.js (v-if v-else v-show)
- [Vue] Preload Data using Promises with Vue.js and Nuxt.js
- vue.js3:用el-checkbox做复选框(vue@3.2.37 / element-plus@2.2.2)
- vue.js 3.2.6 解决swiper动态加载数据时默认显示最后一页(swiper@6.8.4)
- vue.js 3.2.20: 用rem实现移动端和pc的兼容
- typora及vue主题安装
- vue 的elementui中select下拉框多选项-multiple属性
- vue项目报错:migrating.js?2bb5:56 [Element Migrating][ElInput][Event]: click is removed.
- Vue.js实现文字超出指定高度后展开收起功能
- vue.js项目打包报错Error: You appear to be using a native ECMAScript module configuration file
- Vue服务端渲染Nuxt.js实例
- 在vue中引入elementui
- 基于 Vue.js 的移动端组件库mint-ui实现无限滚动加载更多
- 073:vue+openlayers拖拽实现放大所选区域 (DragZoom示例代码)
- 2023前端面试重点知识点总结【详细】css+js+vue+react+小程序+性能优化等等