rxjs

RxJS Subject学习

风流意气都作罢 提交于 2020-04-27 11:57:58
一个Observable的例子 import { interval } from "rxjs"; import { take } from "rxjs/operators"; const interval$ = interval(1000).pipe(take(3)); interval$.subscribe(value => console.log("Observer A get value: " + value)); setTimeout(() => { interval$.subscribe(value => console.log("Observer B get value: " + value)); }, 1000); 输出 Observer A get value: 0 Observer A get value: 1 Observer B get value: 0 Observer A get value: 2 Observer B get value: 1 Observer B get value: 2 可以看到 Observable 对象可以被重复订阅。 Observable 对象每次被订阅后,都会重新执行。 一个Subject的例子 import { interval, Subject } from "rxjs"; import { take } from

Angular v6 正式发布

旧城冷巷雨未停 提交于 2020-04-26 07:34:39
Angular 6 正式发布 Angular 6 已经正式发布了!这个主要版本并不关注于底层的框架,更多地关注于工具链,以及使 Angular 在未来更容易快速推进。 作为发布的一部分,我们同步了主要的框架包 (@angular/core, @angular/common, @angular/compiler, etc), Angular CLI, 以及 Angular Material + CDK。现在全部都是作为 6.0.0 发布。我们理清了它们之间的兼容性。小的更新的补丁将会基于项目的需要发布。 可以通过每个项目的变更清单来查看全部的内容: framework , material+cdk , cli . 1. ng update ng update <package> 是一个新的 CLI 命令,用于分析 package.json 并使用其关于 Angular 的知识来更新您的应用。请查看 升级手册 来查看其行为。 不仅可以使用 ng update 来帮助您适配正确版本的依赖项,并保持依赖同步,而且第三方也可以使用 schematics 来提供 update 脚本命令。如果您的某个依赖库提供了 ng update 语法,在其有大更新的时候,就可以自动更新您的代码了。 ng update 并不会替换您的包管理器,而是使用 npm 或者 yarn 在底层管理依赖,然后更新依赖

Return nested forkjoins as observable

北城以北 提交于 2020-04-18 12:31:24
问题 I'm trying to return a bunch of nested forkjoins and normal subscribes in my resolver. For this I tried using maps, but I think I don't fully grasp the concept of maps/switchMaps/mergeMaps yet. I know the code doesnt return a UserResult yet, this is because I have no idea yet how I will add questionAnswers to the UserResult, but this shouldn't be that much of a difference for my current problem. My goals is to rewrite this, so that it returns an observable. resolve(route:

Get one event per 1000 milliseconds interval

空扰寡人 提交于 2020-04-18 05:36:28
问题 I have infinite stream of events that can emit some consecutive event portions and I want to take one event per 1000 each milliseconds. I tried debounceTime / auditTime / throttleTime but they doesn't include all events I want - to demonstrate the behavior I created playground on stackblitz which fires events once per 300ms within portion of 10 events: debounceTime(1000) will give only event # 10 throttleTime(1000) will give events 1,5,9 but it will omit # 10 which is essential auditTime(1000

Angular Unit Testing - Verify emitted value from an observable using marble testing

ⅰ亾dé卋堺 提交于 2020-04-18 03:45:23
问题 I'm creating some unit tests for an Angular application that I have. I have managed to test services that make HTTP calls without any problems. I now moved to a different kind of service, which is shown below. Code import { Injectable } from '@angular/core'; import { Observable, Subject } from 'rxjs'; import { UICoupon } from '../models'; @Injectable() export class ClaimCouponService { hasClaimedCoupons = false; claimedCoupons: UICoupon[] = []; onClaimCoupons: Observable<UICoupon[]>; private

How do I merge an array of Observables with RxJS 6.x and Node.js?

老子叫甜甜 提交于 2020-04-16 03:56:06
问题 For learning purposes, I'm creating a Node app that will need to take x RxJS observables from an array and combine into a single stream of events. I want to know when events occur in any observable, in any order (not in any sequence or full completion). I feel it should be in a single merged stream of events. Basically, the first event that comes through from any observable will complete. For this, I felt merge() will do the trick. As merge doesn't take arrays directly as a parameter, I'm

Type 'Subscription' is missing the following properties from type 'Observable<StringMap<any>>'

╄→гoц情女王★ 提交于 2020-04-16 02:39:53
问题 ERROR : Type 'Subscription' is missing the following properties from type 'Observable>': _isScalar, source, operator, lift, and 6 more.ts(2740) Here I have attached my code. Here, in my case, I have two methods which return an observable, but getByTypeData and getByType. But, on returning this.getByType(type).. from getByTypeData() I am getting above error. P.S.: I want to subscribe getByTypeData in my component which should return me an observable. AND I AM NEW TO RXJS... /* interface

RXJS redux observable perform multiple api calls

冷暖自知 提交于 2020-04-14 04:38:09
问题 I have an array of contacts and would like to perform an API calls for each object in the contacts array. I have tried to loop over each item and make the call to fetchJson$, however, I realise that this approach is incorrect. What is the correct way to do this with RXJS Redux Observables? Also, this is pseudo code for the purpose of this question so please ignore any syntax errors. export const createPost = (action$, store) => ( action$.ofType(UPDATE_USER_CONTACTS) .switchMap(() => { const

Should I always use Observables where a promise can be used?

℡╲_俬逩灬. 提交于 2020-04-13 14:51:35
问题 I posted a question on stack overflow yesterday where I asked the userbase if there was a way to await the result of an async operation without explicitly using the async and await keywords. The reason being that I needed to wait for the operation to complete so that I could use the value returned before any other HTTP requests could be fired. It turns out there was, it involved using observables to subscribe to the result of the operation, and to then use a mergeMap to continue on with the

Change value in an observable

半城伤御伤魂 提交于 2020-04-13 07:24:09
问题 If I have an observable student: Observable<Student> where Student has the parameter name: string is set to Jim , How can I change the value of name in the students observable to be Bob ? Edit: Is student.map(student => student.name = 'Bob') supposed to work. Because if yes then there is something else wrong with my program. 回答1: @ZahiC's answer is the right one, but let me explain a little bit why. First, with Rxjs, the less side effects you have, the better. Immutability is your friend!