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的更多相关文章

  1. Accessor Search Implementation Details

    [Accessor Search Implementation Details] Key-value coding attempts to use accessor methods to get an ...

  2. 17.2.1 Replication Implementation Details 复制实现细节:

    17.2 Replication Implementation 复制是基于master server 跟踪所有改变到他的数据库(更新,删除等等)在它的binary log. binary log 作为 ...

  3. NgRx/Store 4 + Angular 5使用教程

    这篇文章将会示范如何使用NgRx/Store 4和Angular5.@ngrx/store是基于RxJS的状态管理库,其灵感来源于Redux.在NgRx中,状态是由一个包含action和reducer ...

  4. [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 ...

  5. Angular应用架构设计-3:Ngrx Store

    这是有关Angular应用架构设计系列文章中的一篇,在这个系列当中,我会结合这近两年中对Angular.Ionic.甚至Vuejs等框架的使用经验,总结在应用设计和开发过程中遇到的问题.和总结的经验, ...

  6. angular的跨域(angular百度下拉提示模拟)和angular选项卡

    1.angular中$http的服务: $http.get(url,{params:{参数}}).success().error(); $http.post(url,{params:{参数}}).su ...

  7. angular 2+ 变化检测系列三(Zone.js在Angular中的应用)

    在系列一中,我们提到Zone.js,Zones是一种执行上下文,它允许我们设置钩子函数在我们的异步任务的开始位置和结束位置,Angular正是利用了这一特性从而实现了变更检测. Zones.js非常适 ...

  8. 【Angular JS】正确调用JQuery与Angular JS脚本 - 修复Warning: Tired to load angular more than once

    自己正在做一个小网站,使用Angular JS + Express JS + Mongo DB,在开发过程中,遇到一些问题,所以整理出来.希望对大家都有帮助. 这是今天解决的一个问题,Angular ...

  9. [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 ...

随机推荐

  1. HDU 4607.Park Visit-树的直径(BFS版)+结论公式(乱推公式)-备忘(加油!)

    Park Visit Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. Oracle常用常考集合

    登陆远程服务器 sqlplus scott/tiger@192.168.2.1[:port]/sid [as sysdba] 简单查询 select  table_name from user_tab ...

  3. 深度学习应用系列(三)| autokeras使用入门

    我们在构建自己的神经网络模型时,往往会基于预编译模型上进行迁移学习.但不同的训练数据.不同的场景下,各个模型表现不一,需要投入大量的精力进行调参,耗费相当多的时间才能得到自己满意的模型. 而谷歌近期推 ...

  4. 线段树+扫描线【bzoj1645】[USACO07OPEN]城市的地平线City Horizon

    Description 约翰带着奶牛去都市观光.在落日的余晖里,他们看到了一幢接一幢的摩天高楼的轮廓在地平线 上形成美丽的图案.以地平线为 X 轴,每幢高楼的轮廓是一个位于地平线上的矩形,彼此间可能有 ...

  5. HDU 6071 Lazy Running (同余最短路)

    Lazy Running Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)To ...

  6. Flask实战第56天:板块管理

    cms布局 编辑 cms_boards.html {% block main_content %} <div class="top-box"> <button c ...

  7. JSON APIs and Ajax

    1. 通过jQuery来绑定点击事件. 函数 $(document).ready()这个函数中的代码只会在我们的页面加载时候运行一次,确保执行js之前页面所有的dom已经准备就绪. 在$(docume ...

  8. Oracle REGEXP

    ORACLE中的支持正则表达式的函数主要有下面四个: 1,REGEXP_LIKE :与LIKE的功能相似 2,REGEXP_INSTR :与INSTR的功能相似 3,REGEXP_SUBSTR :与S ...

  9. 旺财C# .NET代码生成器之DTcms4/5代码批量生成功能

    经近半年的实战积累.于2017年6月底,增加对第三方开源软件DTcms(4和5都支持)的代码生成支持. 可以一键生成的DTcms4/5代码如下:1.Model经典三层实体2.DAL经典三层数据库访问层 ...

  10. Thupc2017"礼"?

    题面 先粘上gouzhi的题面,听说是thupc的题 [问题背景] 情人节要到了,zhx 要给女朋友买礼物. [问题描述] 橱窗里摆放了 n 种不同的玩偶,购买第 i 种玩偶需要价格 a[i],价值为 ...