博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Angular] Creating an Observable Store with Rx
阅读量:7035 次
发布时间:2019-06-28

本文共 1676 字,大约阅读时间需要 5 分钟。

The API for the store is really simple:

/*   set(name: string, state: any);   select
(name: string): Observable
*/

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
(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
(name: string): Observable
{ // 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
('todos'); constructor(private store: Store) { this.store.set('todos', [ {id: 1, name: 'Learning Angular'}, {id: 2, name: 'Learning Redux'} ]); }}
  • {
    {todo.name}}

 

转载地址:http://rdyal.baihongyu.com/

你可能感兴趣的文章
mac outlook 自动回复
查看>>
横屏竖屏
查看>>
Zabbix监控MySQL
查看>>
OSChina 周三乱弹 ——祖传的程序员?????
查看>>
OSChina 周五乱弹 —— 埃塞俄比亚的远房大表姐
查看>>
大华设备扫描工具
查看>>
twisted的异步库汇总-- mysql,redis,mongo,zmq,sockjs等
查看>>
Python3 基于asyncio的新闻爬虫思路
查看>>
af3.0学习使用和理解
查看>>
Linux vmstat命令实战详解
查看>>
输入字符串,输出字符串所有组合
查看>>
Python中 字典排序、列表排序
查看>>
ubuntu12.04 安装vnc
查看>>
我的友情链接
查看>>
前嗅ForeSpider脚本教程:基础对象(三)
查看>>
MongoDB的数据复制和数据切片
查看>>
IDEA安装FindBugs插件
查看>>
Thinking in Java之深入Collection源码学习
查看>>
Ceph:一个 Linux PB 级分布式文件系统
查看>>
Red Hat Enterprise 6.3手动安装Thunderbird
查看>>