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. Disruptor源码解读

    上一篇已经介绍了Disruptor是什么?简单总结了为什么这么快?下面我们直接源码搞起来,简单粗暴.高性能队列disruptor为什么这么快? 一.核心类接口 Disruptor 提供了对RingBu ...

  2. Android ListView动态刷新某项Item

    使用ViewHolder来刷新某项数据,而不用每次都全部刷新数据. 继承BaseAdapter,新建ViewHolder类. public class TestListAdapter extends ...

  3. Web性能测试术语

    并发用户: 并发一般分为2种情况.一种是严格意义上的并发,即所有的用户在同一时刻做同一件事情或者操作,这种操作一般指做同一类型的业务.比如在信用卡审批业 务中,一定数目的用户在同一时刻对已经完成的审批 ...

  4. spring中配置数据源

    spring中配置数据源的几种常见方式: #mysql 数据库配置(jdbc.properties) jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.u ...

  5. 以POST方式推送JSON数据,并接收返回的服务器消息

    private static string GetResult(string jsonString, string type) { string url = GetUrl(type); string ...

  6. 03Struts2基本使用流程

    Struts2基本使用流程 1.新建web工程 2.引入struts2类库 3.创建并配置Struts2的核心控制器web.xml用来拦截客户端请求并将请求转发到相应的Action类中来处理 4.创建 ...

  7. 洛谷P1107 & BZOJ1270 [BJWC2008]雷涛的小猫

    一道DP. 给你一个矩阵里面有很多数,你需要从上往下找到一种跳跃方法使得经过的点的价值之和最大. 具体题面见链接 洛谷P1107 BZOJ1270 很明显是一个二维的DP. #include<b ...

  8. 11servlet接口

    11.servlet接口-2018/07/23 1.servlet 是一个接口,需要导包javax.servlet.Servlet; 第一种编写一个servlet程序的方法 写一个Java类,实现se ...

  9. 洛谷——P2018 消息传递

    P2018 消息传递 题目描述 巴蜀国的社会等级森严,除了国王之外,每个人均有且只有一个直接上级,当然国王没有上级.如果A是B的上级,B是C的上级,那么A就是C的上级.绝对不会出现这样的关系:A是B的 ...

  10. Python学习-while循环练习

    1.计算1-100的和 i = 1; total = 0; while i <= 100: total = total + i; i = i + 1; print(total); 2.打印出1- ...