[Angular] Creating an Observable Store with Rx
The API for the store is really simple:
/*
set(name: string, state: any); select<T>(name: string): Observable<T>
*/
There are two methods, set() & select().
Store:
import {Observable} from 'rxjs/Observable';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import {IState} from './state';
import 'rxjs/add/operator/pluck';
import 'rxjs/add/operator/distinctUntilChanged';
const state: IState = {
playList: undefined
};
export class Store {
/*Because store usually has a default value, BehaviorSubject is suitable for that*/
private subject = new BehaviorSubject<IState>(state);
/*Create a observable store*/
private store = this.subject
.asObservable() // convert to an observable
.distinctUntilChanged(); // performance improve
/*Get value from subject*/
get value() {
return this.subject.value;
}
set(name: string, state: any) {
const nextState = {
...this.value,
[name]: state
};
this.subject.next(nextState);
}
select<T>(name: string): Observable<T> {
// get prop value from observable
return this.store.pluck(name);
}
}
interface:
export interface IState {
playList: any[]
}
Component:
import { Component } from '@angular/core';
import {Store} from './store';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
todos$ = this.store.select<any[]>('todos');
constructor(private store: Store) {
this.store.set('todos', [
{id: , name: 'Learning Angular'},
{id: , name: 'Learning Redux'}
]);
}
}
<div>
<ul *ngFor="let todo of todos$ | async">
<li>{{todo.name}}</li>
</ul>
</div>
[Angular] Creating an Observable Store with Rx的更多相关文章
- [Angular 2] Using ngrx/store and Reducers for Angular 2 Application State
ngrx/store is a library that simplifies common RxJS patterns for managing state and gives you an eas ...
- [Angular] How to get Store state in ngrx Effect
For example, what you want to do is navgiate from current item to next or previous item. In your com ...
- [rxjs] Creating An Observable with RxJS
Create an observable var Observable = Rx.Observable; var source = Observable.create(function(observe ...
- Angular基础(八) Observable & RxJS
对于一个应用来说,获取数据的方法可以有很多,比如:Ajax, Websockets, LocalStorage, Indexdb, Service Workers,但是如何整合多种数据源.如何避免BU ...
- angular Creating a Directive that Adds Event Listeners
<span my-draggable>Drag ME</span> angular.module('dragModule', []) .directive('myDraggab ...
- Angular 2.0 从0到1 (七)
第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2.0 从0到1 (三)第四节:Angular 2.0 从0到1 (四)第五节: ...
- Angular 2.0 从0到1 (六)
第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2.0 从0到1 (三)第四节:Angular 2.0 从0到1 (四)第五节: ...
- Angular 2.0 从0到1 (四)
第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2.0 从0到1 (三)第四节:Angular 2.0 从0到1 (四)第五节: ...
- Angular - 预加载 Angular 模块
Angular - 预加载延迟模块 在使用路由延迟加载中,我们介绍了如何使用模块来拆分应用,在访问到这个模块的时候, Angular 加载这个模块.但这需要一点时间.在用户第一次点击的时候,会有一点延 ...
随机推荐
- tomcat:web容器
Web容器和应用服务器除了你所说的request response之外还有很多其他重要的功能. 1. 多线程和并发处理,估计你写的socket程序支持不了几个并发访问吧 2. 安全性 3. 生命周期管 ...
- Vue 导出表格为Excel
放法有多种,我这里是直接转JSON数据为Excel. 1.既然要使用,那首先当然是安装依赖,在终端命令输入: npm install -S file-saver xlsx npm install -D ...
- Python day4知识回顾
# -*- coding: utf_8 _*_# Author:Vi#字典是无序的 info = { 'student001':"DIO", 'student002':" ...
- matplotlib散点图笔记
定义: 由一组不连续的点完成的图形 散点图: 包含正相关性,负相关性和不相关性. 散点图生成函数: plt.scatter(x,y) 演示代码如下: import numpy as np import ...
- Linux搭建aspx.net环境之:CentOs 7 安装 Mono 和 Jexus 步骤记录
1 因为163没有CentOs7的镜像.所以没有加这个 wget http://mirrors.163.com/.help/CentOS6-Base-163.repo cd /etc/yum.rep ...
- swift 一疑问:reduce
var products = [ Product(name: "Kayak", description: "A boat for one person",p ...
- 学习WWDC的好资源!
学习WWDC的好资源. 大家都知道.要看Apple每年一度的WWDC,仅仅要到它的Developer站点去就能够了.那里有每年的研讨会视频,并且还能够下载每一个视频的SD或HD视频文件,以及相关的演示 ...
- C# 中编写函数式代码
http://www.dotnetcurry.com/csharp/1384/functional-programming-fsharp-for-csharp-developers 写给 C# 开发人 ...
- 轻松学习Linux之用户账户管理及实例
Linux用户管理基础 (下载清晰视频:http://down.51cto.com/data/158699) 轻松学习Linux之用户账户管理的实例-跨硬盘移动数据 (此处视频不清楚下按下面地址下载清 ...
- 深入了解Linux远程桌面
本文转载于:http://www.linux521.com/2009/system/201004/11001.html 已发表在<网管员世界>2010年3月杂志 本 ...