Wrapping the creation of an Observable inside of a Function allows you delay the creation of the Observable until it is needed. This becomes really important when the Observable is created from a Promise due to the way Promises execute on creation.

Current Code:

<template>
<section class="section">
<button class="button" v-stream:click="click$">Click</button>
<h2>
{{name$}}
</h2>
<img v-stream:error="imageError$" :src="data:image$" alt="">
</section>
</template> <script>
import { from, merge } from 'rxjs';
import { switchMap, pluck, map, mapTo } from 'rxjs/operators'; export default {
name: 'app',
domStreams: ['click$', 'imageError$'],
subscriptions() { const person$ = from(
this.$http.get(
"https://starwars.egghead.training/people/1"
)
).pipe(
pluck('data')
) const luke$ = this.click$.pipe(
switchMap(() => person$)
) const name$ = luke$.pipe(
pluck('name')
) const loadImage$ = luke$.pipe(
pluck('image'),
map(src => `https://starwars.egghead.trainin/${src}` )
) const failImage$ = this.imageError$
.pipe(
mapTo(`http://via.placeholder.com/400x400`)
) const image$ = merge(
loadImage$,
failImage$
) return {
name$,
image$
}
}
};
</script>

The highlight part of code will be run once page loaded. So it means before the button was clicked, we already send a network request.

To defer the network request, we can do:

<template>
<section class="section">
<button class="button" v-stream:click="click$">Click</button>
<h2>
{{name$}}
</h2>
<img v-stream:error="imageError$" :src="data:image$" alt="">
</section>
</template> <script>
import { from, merge } from 'rxjs';
import { switchMap, pluck, map, mapTo } from 'rxjs/operators'; export default {
name: 'app',
domStreams: ['click$', 'imageError$'],
subscriptions() { const loadPerson = (url) => from(
this.$http.get(
url
)
).pipe(
pluck('data')
) const luke$ = this.click$.pipe(
mapTo("https://starwars.egghead.training/people/1"),
switchMap((url) => loadPerson(url))
); 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$
) return {
name$,
image$
}
}
};
</script>

[Vue-rx] Switch to a Function which Creates Observables with Vue.js and Rxjs的更多相关文章

  1. vue源码逐行注释分析+40多m的vue源码程序流程图思维导图 (diff部分待后续更新)

    vue源码业余时间差不多看了一年,以前在网上找帖子,发现很多帖子很零散,都是一部分一部分说,断章的很多,所以自己下定决定一行行看,经过自己坚持与努力,现在基本看完了,差ddf那部分,因为考虑到自己要换 ...

  2. 【js】vue 2.5.1 源码学习 (三) Vue.extend 和 data的合并策略

    大体思路 (三)  1. 子类父类  2.Vue.extend()      //创建vue的子类 组件的语法器 Vue.extend(options) Profile().$mount('#app' ...

  3. vue新手入门指导,一篇让你学会vue技术栈,本人初学时候的文档

    今天整理文档突然发现了一份md文档,打开一看 瞬间想起当年学习vue的艰难路,没人指导全靠自己蒙,下面就是md文档内容,需要的小伙伴可以打开个在线的md编译器看一看,我相信不管是新人还是老人  入门总 ...

  4. Vue引入第三方JavaScript库和如何创建自己的Vue插件

    一 第三方JavaScript库 前言 .vue文件 中不解析 script标签引入js文件,只能用 import 引入 有两种用法: 1.import a from '../a' 2.import ...

  5. 循序渐进VUE+Element 前端应用开发(7)--- 介绍一些常规的JS处理函数

    在我们使用VUE+Element 处理界面的时候,往往碰到需要利用JS集合处理的各种方法,如Filter.Map.reduce等方法,也可以设计到一些对象属性赋值等常规的处理或者递归的处理方法,以前对 ...

  6. Vue源码分析之实现一个简易版的Vue

    目标 参考 https://cn.vuejs.org/v2/guide/reactivity.html 使用 Typescript 编写简易版的 vue 实现数据的响应式和基本的视图渲染,以及双向绑定 ...

  7. jquery TypeError: 'undefined' is not a function (evaluating 'elem.nodeName.toLowerCase()') [jquery.js:1904]错误原因

    今天,某个环境报了个js错误,TypeError: 'undefined' is not a function (evaluating 'elem.nodeName.toLowerCase()') [ ...

  8. Vue路由器的hash和history两种工作模式 && Vue项目编译部署

    1 # 一.Vue路由器的两种工作模式 2 # 1.对于一个uri来说,什么是hash值? 井号及其后面的内容就是hash值. 3 # 2.hash值不会包括含在HTTP请求中,即:hash值不会带给 ...

  9. Vue 搭建脚手架 && 脚手架的文件结构 && 关于不同版本的Vue

    1 # 一.Vue 环境搭建 2 # 1.VsCode 编码插件:Vuter 3 # 2.Vue 脚手架安装 4 # 1).安装:npm install -g @vue/cli or yarn glo ...

随机推荐

  1. JSP&&EL&&JSTL

    JSP = JAVA + HTML + JSP自身的东西 JSP的脚本         <%!   %>        :翻译成Servlet中的成员内容. 定义变量,方法,类. -- ...

  2. LN : leetcode 3 Longest Substring Without Repeating Characters

    lc 3 Longest Substring Without Repeating Characters 3 Longest Substring Without Repeating Characters ...

  3. HTML城市联动

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. hihocoder offer收割编程练习赛13 D 骑士游历

    思路: 矩阵快速幂. 实现: #include <iostream> #include <cstdio> #include <vector> using names ...

  5. droid开发:如何打开一个.dcm文件作为位图?

    我目前正在做一个Android应用程序的DICOM 继code打开图片DROM RES /绘制的“ussual”图像格式,但它不与.dcm工作 公共类BitmapView扩展视图 { 公共Bitmap ...

  6. python的机器学习之路

    2018-04-1712:22:40 这是python依靠计算机视觉进行的ocr手写字的识别. 通过KNN训练数据 kNN 可以说是最简单的监督学习分类器了.想法也很简单,就是找出测试数据在特征空间中 ...

  7. Jmeter接口测试---JDBC简单实践

    我的环境:MySQL:mysql-5.6.24-win32 jdbc驱动:mysql-connector-java-5.1.22-bin.jar JMeter:apache-jmeter-2.13 1 ...

  8. Tomcat环境的搭建

    一.Tomcat的简单介绍 大家应该知道平时所说的C/S和B/S系统架构:C/S架构是基于客户端C和服务端S的,B/S架构是基于浏览器B和S服务端的,B/S架构中的server就是web服务器. To ...

  9. 【原】CentOS release 6.2 安装mysql

     1.  yum update升级以后的系统版本为 [root@yl-web yl]# cat /etc/redhat-release CentOS Linux release 7.1.1503 (C ...

  10. @ExceptionHandler和@ControllerAdvice统一处理异常

    //@ExceptionHandler和@ControllerAdvice统一处理异常//统一处理异常的controller需要放在和普通controller同级的包下,或者在ComponentSca ...