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. Python学习日记之练习代码

    # -*- coding:utf-8 -*- number = 23 test=True while test: guess=int(raw_input('输入数字')) if guess==numb ...

  2. MySQL 8.*版本 修改root密码

    MySQL .*版本 修改root密码 查看版本:select version() from dual; 1.6. 登录mysql: 登录mysql:(因为之前没设置密码,所以密码为空,不用输入密码, ...

  3. vue组件---边界处理情况

    (1)访问元素&组件 ①访问根实例 在每个 new Vue 实例的子组件中,其根实例可以通过 $root 属性进行访问.例如,在这个根实例中: // Vue 根实例 new Vue({ dat ...

  4. java基础学习日志--String、StringBuffer方法案例

    package StringDemo; import java.util.Arrays; /* * 常用String.StringBufer类的方法 */ public class Demo1 { p ...

  5. 安装ubuntu系统空间分配问题

    以下是我安装linux系统(ubuntu)时的系统空间配置,以50G为例: 挂载点 大小 格式 分区类型 / 15G Ext4 主分区 /home 30G Ext4 逻辑分区 /boot 1G Ext ...

  6. JS中遍历EL表达式中后台传过来的Java集合

    前言:在我的项目里有这么一个情况,后台直接model.addAttribute()存储了一个对象,此对象内部有一个集合,前端JSP处理的方法正常情况下就是直接使用EL表达式即可.但是如果在JS中需要使 ...

  7. bootstrap-table使用笔记

    服务端分页: //html <div class="container-fluid"> <div style="margin-top:1em" ...

  8. flex多列布局遇到的问题,和解决方案

    flex布局无疑是简单.易用的,他让我我们的布局更加简单和快速,但是在使用flex进行多列布局的时候,我相信很多人会遇到下面的情况: 这种情况是因为我们使用了justify-content: spac ...

  9. 根据屏幕的大小改变rem的参考值

    移动端一半会选用rem+flex布局的方式,下面是根据屏幕的宽度,动态的改变rem的参考值 var screenWidth;             var html = document.getEl ...

  10. poj 3744 Scout YYF I(递推求期望)

    poj 3744 Scout YYF I(递推求期望) 题链 题意:给出n个坑,一个人可能以p的概率一步一步地走,或者以1-p的概率跳过前面一步,问这个人安全通过的概率 解法: 递推式: 对于每个坑, ...