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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [rxjs] Creating An Observable with RxJS

    Create an observable var Observable = Rx.Observable; var source = Observable.create(function(observe ...

  4. Angular基础(八) Observable & RxJS

    对于一个应用来说,获取数据的方法可以有很多,比如:Ajax, Websockets, LocalStorage, Indexdb, Service Workers,但是如何整合多种数据源.如何避免BU ...

  5. angular Creating a Directive that Adds Event Listeners

    <span my-draggable>Drag ME</span> angular.module('dragModule', []) .directive('myDraggab ...

  6. 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 (四)第五节: ...

  7. 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 (四)第五节: ...

  8. 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 (四)第五节: ...

  9. Angular - 预加载 Angular 模块

    Angular - 预加载延迟模块 在使用路由延迟加载中,我们介绍了如何使用模块来拆分应用,在访问到这个模块的时候, Angular 加载这个模块.但这需要一点时间.在用户第一次点击的时候,会有一点延 ...

随机推荐

  1. java+spark-sql查询excel

    Spark官网下载Spark Spark下载,版本随意,下载后解压放入bigdata下(目录可以更改) 下载Windows下Hadoop所需文件winutils.exe 同学们自己网上找找吧,这里就不 ...

  2. nodejs基础部分(一)

    前言 业余时间充实自我,入手学习了解一下传说中纯事件驱动/非阻塞的js架构 --nodejs 好记性不如烂笔头,本系列随笔用于整理记录学习nodejs过程中的心得 目录 nodejs简介 nodejs ...

  3. 使用spring-boot 国际化配置所碰到的乱码问题

    写好html静态页面 ,  也加上了编码格式 , 获取国际化展示在浏览器中还是存在乱码 , 开始以为是浏览器编码格式问题 , 做过处理后任没有得到解决 , 具体的处理方案如下: <meta ht ...

  4. CodeForces 159c String Manipulation 1.0

    String Manipulation 1.0 Time Limit: 3000ms Memory Limit: 262144KB This problem will be judged on Cod ...

  5. springboot整合Beetl、BeetlSql实现ajax分页

    Beetl是Bee Template Language的缩写,它绝不是简单的另外一种模板引擎,而是新一代的模板引擎,它功能强大,性能良好,超过当前流行的模板引擎.而且还易学易用. BeetSql是一个 ...

  6. [Javascript] Classify JSON text data with machine learning in Natural

    In this lesson, we will learn how to train a Naive Bayes classifier and a Logistic Regression classi ...

  7. 自己定义控件的onMeasure方法具体解释

    在我们自己定义控件的时候可能你会用到onMeasure方法,以下就具体的给大家介绍一下这种方法: @Override protected void onMeasure(int widthMeasure ...

  8. Day6下午题解1

    预计分数:100+?+30=130+? 实际分数:100+25+30=155 T1 https://www.luogu.org/problem/show?pid=T15920 DP裸题,用dp[i][ ...

  9. Atcoder AtCoder Regular Contest 079 E - Decrease (Judge ver.)

    E - Decrease (Judge ver.) Time limit : 2sec / Memory limit : 256MB Score : 600 points Problem Statem ...

  10. 笔记四:onsubmit和onclick的区别

    今天碰到关于表单提交的问题,我是用submit还是用onclick好呢,然后我去百度了一下两者的区别: onsubmit只能表单上使用,提交表单前会触发, onclick是按钮等控件使用, 用来触发点 ...