[Angular] Reactive Store and AngularFire Observables
A simple store implemenet:
import { Observable } from 'rxjs/Observable';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import 'rxjs/add/operator/pluck';
import 'rxjs/add/operator/distinctUntilChanged';
import {User} from './auth/shared/services/auth/auth.service';
export interface State {
user: User;
[key: string]: any
}
const state: State = {
user: undefined
};
export class Store {
private subject = new BehaviorSubject<State>(state);
private store = this.subject.asObservable().distinctUntilChanged();
get value() {
return this.subject.value;
}
select<T>(name: string): Observable<T> {
return this.store.pluck(name);
}
set(name: string, state: any) {
this.subject.next({ ...this.value, [name]: state });
}
}
Using this store in AuthService:
import {Injectable} from '@angular/core';
import {AngularFireAuth} from 'angularfire2/auth';
import {Store} from 'store';
import 'rxjs/add/operator/do';
export interface User {
uid: string;
email: string;
authenticated: boolean;
}
@Injectable()
export class AuthService {
// handle on every auth state changes
auth$ = this.af.authState
.do(next => {
if (!next) {
this.store.set('user', null);
return;
}
const user = {
email: next.email,
uid: next.uid,
authenticated: true
};
this.store.set('user', user);
});
constructor(
private af: AngularFireAuth,
private store: Store
) {
}
createUser(email: string, password: string) {
return this.af.auth.createUserWithEmailAndPassword(email, password);
}
loginUser(email: string, password: string) {
return this.af.auth.signInWithEmailAndPassword(email, password)
}
}
Using Reactive approach in app.component.ts:
import {Component, OnDestroy, OnInit} from '@angular/core';
import {Store} from 'store';
import {AuthService} from '../../../auth/shared/services/auth/auth.service';
import {Observable} from 'rxjs/Observable';
import {Subscription} from 'rxjs/Subscription';
import {User} from 'firebase/app';
@Component({
selector: 'app-root',
styleUrls: ['app.component.scss'],
template: `
<div>
<h1>{{user$ | async | json}}</h1>
<div class="wrapper">
<router-outlet></router-outlet>
</div>
</div>
`
})
export class AppComponent implements OnInit, OnDestroy{
user$: Observable<User>;
subscription: Subscription;
constructor(
private store: Store,
private authService: AuthService
) {}
ngOnInit() {
this.subscription = this.authService.auth$.subscribe();
this.user$ = this.store.select<User>('user');
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
[Angular] Reactive Store and AngularFire Observables的更多相关文章
- angular reactive form
这篇文章讲了angular reactive form, 这里是angular file upload 组件 https://malcoded.com/posts/angular-file-uploa ...
- [Angular] Reactive Form -- FormControl & formControlName, FormGroup, formGroup & formGroupName
First time dealing with Reactive form might be a little bit hard to understand. I have used Angular- ...
- Angular Reactive Form-响应式表单验证
内建验证规则 Angular中提供了一些內建的Validators,这些验证规则可以在Template-Driven或Reactive表单中使用. 目前 Angular 支持的内建 validator ...
- Angular Reactive Forms -- Model-Driven Forms响应式表单
Angular 4.x 中有两种表单: Template-Driven Forms - 模板驱动式表单 (类似于 AngularJS 1.x 中的表单 ) 官方文档:https://v2.angul ...
- Angular Reactive Form - 填充表单模型
setValue 使用setValue,可以通过传递其属性与FormGroup后面的表单模型完全匹配的数据对象来一次分配每个表单控件值. 在分配任何表单控件值之前,setValue方法会彻底检查数据对 ...
- 手把手教你用ngrx管理Angular状态
本文将与你一起探讨如何用不可变数据储存的方式进行Angular应用的状态管理 :ngrx/store——Angular的响应式Redux.本文将会完成一个小型简单的Angular应用,最终代码可以在这 ...
- angular版聊天室|仿微信界面IM聊天|NG2+Node聊天实例
一.项目介绍 运用angular+angular-cli+angular-router+ngrx/store+rxjs+webpack+node+wcPop等技术实现开发的仿微信angular版聊天室 ...
- 2015,2016 Open Source Yearbook
https://opensource.com/yearbook/2015 The 2015 Open Source Yearbook is a community-contributed collec ...
- [AngularJS Ng-redux] Integrate ngRedux
Up to this point, we have created an effective, yet rudimentary, implementation of Redux by manually ...
随机推荐
- 对string的一些扩展函数
对string作了一些扩展,包含string转化为int.string转化为double.string转化为bool.打印系统当前时间.但没有解决数据溢出的问题,请大神帮忙解决! //头文件 /*pa ...
- SWTBOK測试实践系列(1) -- 測试在项眼下期的评审投入划算吗?
測试策略:静态測试还是动态測试? [对话场景] 成功公布某个软件版本号之后,项目团队召开了项目的经验教训总结大会.在会议期间,项目经理小项和測试经理小測进行了例如以下的对话: 小项:"小測, ...
- Android_L(64bit) 模拟器配置及创建项目
Android L可能就是Android 5.0.随之而来的还有Android Watch. Android TV. 而据说在10月中旬也就是15号Google的公布会上应该会推出Nexus 6和Ne ...
- 懒加载js实现和优化
1.懒加载的作用和原理 在我们展示多图片的场景下,类似淘宝或者百度图片,由于图片的数目过多,全部从服务器请求会给用户糟糕的用户体验,为了提升用户体验,我们这里使用懒加载,随着下拉逐步加载. 每个图片的 ...
- POJ 2133 暴搜
题意: 思路: 按照题意暴搜 注意 如果目标串==给的串 答案是2 //By SiriurRen #include <cstdio> #include <cstring> #i ...
- (转)OpenCV 基本知识框架
以下是对<学习OpenCV>一书知识框架的简单梳理 转自:http://blog.chinaunix.net/uid-8402201-id-2899695.html 一.基础操作 ...
- Vue 学习记录<2>
一.Vue https://vue-loader.vuejs.org/zh-cn/ https://vuejs-templates.github.io/webpack/structure.html
- Oracle 10g 10.2.0.1 在Oracle Linux 5.4 32Bit RAC安装手冊(一抹曦阳)
Oracle 10g 10.2.0.1 在Oracle Linux 5.4 32Bit RAC安装手冊(一抹曦阳).pdf下载地址 ,step by step http://download.csdn ...
- struct数组初始化
const int MAXN=100; struct A { int a,b; }; struct A arr[100];//此时编译通过 struct A arr[MAXN];//此时编译不通过,原 ...
- SDUT--Pots(二维BFS)
Pots Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 You are given two pots, having the ...