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();
}
}

最新文章

  1. SQL-表的各种查查查
  2. WCF 扩展一:格式化Web服务请求XML
  3. (转)Windows驱动编程基础教程
  4. nginx往后端转发时需要注意的两个问题
  5. install g++ on windows
  6. TFboy养成记 CNN
  7. C++技术问题总结-第8篇 STL内存池是怎么实现的
  8. 自学Zabbix3.10.1.2-事件通知Notifications upon events-媒介类型SMS
  9. Hello TensorFlow
  10. linux设备驱动程序--类class的实现
  11. ytkah常用网址导航 关于网站运营等
  12. npm WARN enoent ENOENT: no such file or directory, open &#39;C:\Users\package.json&#39;
  13. STL 序列容器
  14. delphi reintroduce作用
  15. 软件开发中 SQL SERVER 任务的用法
  16. class文件打包成jar
  17. Spring Data JPA、MyBatis还有Hibernate有什么区别
  18. vRealize Automation部署虚机如果出错怎么办?
  19. Tomcat应用的部署记录
  20. 服务 在初始化安装时发生异常:System.IO.FileNotFoundException: 未能加载文件或******

热门文章

  1. 63.note.js之 Mongodb在Nodejs上的配置及session会话机制的实现
  2. ubuntu下eclipse java ee首次打开提示找不到jdk的问题
  3. Fragment-Transaction 源码分析
  4. Repeater控件的
  5. 经典的横线中间文字css布局---flex布局
  6. node搭建服务器
  7. linux awk函数
  8. View_01_LayoutInflater的原理、使用方法
  9. bug 7715339 登录失败触发 ‘row cache lock’ 等待
  10. SDUT--Pots(二维BFS)