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. ssh-agent && 及 ssh-add介绍

    ssh-agent命令是一种控制用来保存公钥身份验证所使用的私钥的程序.ssh-agent在X会话或登录会话之初启动,所有其他窗口或程序则以客户端程序的身份启动并加入到ssh-agent程序中.通过使 ...

  2. Jquery学习总结(1)——Jquery常用代码片段汇总

    1. 禁止右键点击 ? 1 2 3 4 5 $(document).ready(function(){     $(document).bind("contextmenu",fun ...

  3. webclient类学习

    (HttpWebRequest模拟请求登录):当一些硬件设备接口 或需要调用其他地方的接口时,模拟请求登录获取接口的数据就很有必要. webclient类:只想从特定的URI请求文件,则使用WebCl ...

  4. APACHE2.4 指定目录中的字符编码

    APACHE2.4 指定目录中的字符编码 xampp 的 apache2.4 默认字符编码是西文,中文字符显示乱码,在 httpd.conf 没有 AddDefaultCharset utf-8 这样 ...

  5. JS ajax 应用 (下拉列表联动)

        <script language="javascript"> var http_request=false;    function send_request( ...

  6. jQuery高级选择器和其等价方法

    jQuery选择器和css一样,但兼容性更好 <body> <p>p1</p> <p>p1</p> <p>p1</p> ...

  7. 用djbdns为域名解析服务护航

      上期回顾:http://chenguang.blog.51cto.com/350944/292195       650) this.width=650;" alt="&quo ...

  8. vue移动端上拉加载更多

    LoadMore.vue <template> <div class="load-more-wrapper" @touchstart="touchSta ...

  9. Codeforces 919F. A Game With Numbers(博弈论)

      Imagine that Alice is playing a card game with her friend Bob. They both have exactly 88 cards and ...

  10. 【例题 8-2 UVA-1605】Building for UN

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 两层 然后n*n就够了 第一层类似 aaa.. bbb.. ccc.. ... 第二次则变成 abc.... abc.... abc ...