[Angular] Extract Implementation Details of ngrx from an Angular Application with the Facade Pattern
Extracting away the implementation details of ngrx from your components using the facade pattern creates some interesting possibilities in terms of iteratively migrating an application to ngrx. By decoupling your components from ngrx completely, this also makes testing and collaboration a lot easier. In this lesson, we will finalize our application by creating a facade to sit in between our component and ngrx and hide all of the ngrx implementation details away from our component. This has an added benefit of reducing the number of pieces that we need to export in our state module's barrel roll by reducing our dependency down to a single facade.
Current we have the component code like this.. we want to extract implementation detail into facade partten.
component:
import { Component, Input, OnInit, ChangeDetectionStrategy } from '@angular/core';
import { Observable } from 'rxjs'; import {Role} from '../stores/models';
import { Store, select } from '@ngrx/store';
import { DashbaordState, selectAllRoles, LoadRoles } from '../stores'; @Component({
selector: 'dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class DashboardComponent implements OnInit { roles$: Observable<Role[]>;
constructor(
private store: Store<DashbaordState>
) {
this.roles$ = this.store.pipe(
select(selectAllRoles)
);
} ngOnInit() {
this.store.dispatch(new LoadRoles());
} }
Create a facade.ts file:
import { Injectable } from "@angular/core";
import { Store, select } from '@ngrx/store';
import { DashbaordState } from '../reducers';
import { Observable } from 'rxjs';
import { Role } from '../models';
import { selectAllRoles } from '../selectors';
import { LoadRoles } from '../actions'; @Injectable({
providedIn: 'root'
})
export class DashboardFacade {
roles$: Observable<Role[]>;
constructor(
private store: Store<DashbaordState>
) {
this.roles$ = store.pipe(
select(selectAllRoles)
);
} getRoles () {
this.store.dispatch(new LoadRoles());
}
}
Basiclly just move all the Store related code to the facede service.
Update the component:
import { Component, Input, OnInit, ChangeDetectionStrategy } from '@angular/core';
import { Observable } from 'rxjs'; import {Role} from '../stores/models';
import { Store, select } from '@ngrx/store';
import { DashbaordState, selectAllRoles, LoadRoles } from '../stores';
import { DashboardFacade } from '../stores/facades/dashboard.facade'; @Component({
selector: 'dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class DashboardComponent implements OnInit { roles$: Observable<Role[]>; constructor(
private facade: DashboardFacade
) {
this.roles$ = this.facade.roles$;
} ngOnInit() {
this.facade.getRoles();
}
}
[Angular] Extract Implementation Details of ngrx from an Angular Application with the Facade Pattern的更多相关文章
- Accessor Search Implementation Details
[Accessor Search Implementation Details] Key-value coding attempts to use accessor methods to get an ...
- 17.2.1 Replication Implementation Details 复制实现细节:
17.2 Replication Implementation 复制是基于master server 跟踪所有改变到他的数据库(更新,删除等等)在它的binary log. binary log 作为 ...
- NgRx/Store 4 + Angular 5使用教程
这篇文章将会示范如何使用NgRx/Store 4和Angular5.@ngrx/store是基于RxJS的状态管理库,其灵感来源于Redux.在NgRx中,状态是由一个包含action和reducer ...
- [Angular] Improve Server Communication in Ngrx Effects with NX Data Persistence in Angular
Communicating with a remote server via HTTP presents an extra level of complexity as there is an inc ...
- Angular应用架构设计-3:Ngrx Store
这是有关Angular应用架构设计系列文章中的一篇,在这个系列当中,我会结合这近两年中对Angular.Ionic.甚至Vuejs等框架的使用经验,总结在应用设计和开发过程中遇到的问题.和总结的经验, ...
- angular的跨域(angular百度下拉提示模拟)和angular选项卡
1.angular中$http的服务: $http.get(url,{params:{参数}}).success().error(); $http.post(url,{params:{参数}}).su ...
- angular 2+ 变化检测系列三(Zone.js在Angular中的应用)
在系列一中,我们提到Zone.js,Zones是一种执行上下文,它允许我们设置钩子函数在我们的异步任务的开始位置和结束位置,Angular正是利用了这一特性从而实现了变更检测. Zones.js非常适 ...
- 【Angular JS】正确调用JQuery与Angular JS脚本 - 修复Warning: Tired to load angular more than once
自己正在做一个小网站,使用Angular JS + Express JS + Mongo DB,在开发过程中,遇到一些问题,所以整理出来.希望对大家都有帮助. 这是今天解决的一个问题,Angular ...
- [Angular] Step-By-Step Implementation of a Structural Directive - Learn ViewContainerRef
For example we have two buttons: When we click nether one of those tow button, the modal should show ...
随机推荐
- 关于在webapi + ef + 视图 + top查询的问题
在ef中使用视图中有一种坑是视图没有主键,表现的形式是有些数据会出现重复,解决的方法是手动在视图中添加主键即可 在实际的项目中碰到另一种坑,即使用webapi查询时的一种,现记录情况如下: 1:随便创 ...
- linux 下 php 扩展
首先查看,php 原来安装的配置 /www/server/php/72/bin/php -i | grep configure (php 位置) 加上你的扩展 '--with-mcrypt' make ...
- STL容器 -- Priority_Queue
核心:和队列相似,但优先队列中的 “下一个元素” 指的是 “优先级最高” 的元素. 头文件:#include<queue> 普通类型的构造方法: priority_queue<int ...
- Trie树【UVA11362】Phone List
Description 给定\(n\)个长度不超过\(10\)的数字串,判断是否有两个字符串\(A\)和\(B\),满足\(A\)是\(B\)的前缀,若有,输出NO,若没有,输出YES. 一道\(Tr ...
- javascrip异步问题
for ( var i = 1; i <= 3; i++) { setTimeout( function (){ console.log(i); }, 0); }; 一般人会以为输出结果 ...
- 【计算几何】【凸包】Gym - 101164H - Pub crawl
平面上n个点,点之间沿直线走,规划一条路线,每次只能往左半平面的点走,走过最多的点. 显然所有的点都能走过. n^2的暴力显然是每次找左边与其所形成夹角最小的点,但这样过不了(卡常数?). 或者每轮不 ...
- bzoj 4195: [Noi2015]程序自动分析
4195: [Noi2015]程序自动分析 Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,…代表 ...
- Problem B: 指针:调用自定义交换函数,完成5个浮点数从小到大排列
#include<stdio.h> int swap(float *p1,float *p2) { float flag; if(*p1>*p2) { flag=*p1; *p1=* ...
- Manthan, Codefest 16 G. Yash And Trees dfs序+线段树+bitset
G. Yash And Trees 题目连接: http://www.codeforces.com/contest/633/problem/G Description Yash loves playi ...
- WiFi安全测试工具、蹭网利器–WiFiPhisher(转)
读后感:看了一下官方介绍,需要2张无线网卡的支持,其中一张应该是用来影响用户和正常热点的连接,即进行dos攻击,而另外一张可以模拟一个假AP等待用户接入,这种攻击将对物联网和智能家居安防等产品造成很大 ...