ngRx 官方示例分析 - 4.pages
Page 中通过构造函数注入 Store,基于 Store 进行数据操作。
注意 Component 使用了 changeDetection: ChangeDetectionStrategy.OnPush.
OnPush means that the change detector's mode will be set to CheckOnce during hydration.
/app/containers/collection-page.ts
import 'rxjs/add/operator/let';
import { Component, ChangeDetectionStrategy } from '@angular/core';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs/Observable'; import * as fromRoot from '../reducers';
import { Book } from '../models/book'; @Component({
selector: 'bc-collection-page',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<md-card>
<md-card-title>My Collection</md-card-title>
</md-card> <bc-book-preview-list [books]="books$ | async"></bc-book-preview-list>
`,
/**
* Container components are permitted to have just enough styles
* to bring the view together. If the number of styles grow,
* consider breaking them out into presentational
* components.
*/
styles: [`
md-card-title {
display: flex;
justify-content: center;
}
`]
})
export class CollectionPageComponent {
books$: Observable<Book[]>; constructor(store: Store<fromRoot.State>) {
this.books$ = store.select(fromRoot.getBookCollection);
}
}
/app/containers/find-book-page.ts
import 'rxjs/add/operator/let';
import 'rxjs/add/operator/take';
import { Component, ChangeDetectionStrategy } from '@angular/core';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs/Observable'; import * as fromRoot from '../reducers';
import * as book from '../actions/book';
import { Book } from '../models/book'; @Component({
selector: 'bc-find-book-page',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<bc-book-search [query]="searchQuery$ | async" [searching]="loading$ | async" (search)="search($event)"></bc-book-search>
<bc-book-preview-list [books]="books$ | async"></bc-book-preview-list>
`
})
export class FindBookPageComponent {
searchQuery$: Observable<string>;
books$: Observable<Book[]>;
loading$: Observable<boolean>; constructor(private store: Store<fromRoot.State>) {
this.searchQuery$ = store.select(fromRoot.getSearchQuery).take(1);
this.books$ = store.select(fromRoot.getSearchResults);
this.loading$ = store.select(fromRoot.getSearchLoading);
} search(query: string) {
this.store.dispatch(new book.SearchAction(query));
}
}
注意,点击搜索之后,我们回派发一个 Search 的 Action,但是,在 Book 的 Reducer 中并不处理这个 Action, @ngrx/effect 将会监控这个 Action,进行异步处理。
/app/containers/not-found-page.ts
import { Component, ChangeDetectionStrategy } from '@angular/core';
@Component({
selector: 'bc-not-found-page',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<md-card>
<md-card-title>404: Not Found</md-card-title>
<md-card-content>
<p>Hey! It looks like this page doesn't exist yet.</p>
</md-card-content>
<md-card-actions>
<button md-raised-button color="primary" routerLink="/">Take Me Home</button>
</md-card-actions>
</md-card>
`,
styles: [`
:host {
text-align: center;
}
`]
})
export class NotFoundPageComponent { }
通过 @Input() 参数将数据从页面传递给下面的 Component,事件从底层 Component 冒泡上来。
/app/containers/selected-book-page.ts
import { Component, ChangeDetectionStrategy } from '@angular/core';
import { Store } from '@ngrx/store';
import { Observable } from 'rxjs/Observable';
import * as fromRoot from '../reducers';
import * as collection from '../actions/collection';
import { Book } from '../models/book';
@Component({
selector: 'bc-selected-book-page',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<bc-book-detail
[book]="book$ | async"
[inCollection]="isSelectedBookInCollection$ | async"
(add)="addToCollection($event)"
(remove)="removeFromCollection($event)">
</bc-book-detail>
`
})
export class SelectedBookPageComponent {
book$: Observable<Book>;
isSelectedBookInCollection$: Observable<boolean>;
constructor(private store: Store<fromRoot.State>) {
this.book$ = store.select(fromRoot.getSelectedBook);
this.isSelectedBookInCollection$ = store.select(fromRoot.isSelectedBookInCollection);
}
addToCollection(book: Book) {
this.store.dispatch(new collection.AddBookAction(book));
}
removeFromCollection(book: Book) {
this.store.dispatch(new collection.RemoveBookAction(book));
}
}
/app/containers/view-book-page.ts
import '@ngrx/core/add/operator/select';
import 'rxjs/add/operator/map';
import { Component, OnDestroy, ChangeDetectionStrategy } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Store } from '@ngrx/store';
import { Subscription } from 'rxjs/Subscription'; import * as fromRoot from '../reducers';
import * as book from '../actions/book'; /**
* Note: Container components are also reusable. Whether or not
* a component is a presentation component or a container
* component is an implementation detail.
*
* The View Book Page's responsibility is to map router params
* to a 'Select' book action. Actually showing the selected
* book remains a responsibility of the
* SelectedBookPageComponent
*/
@Component({
selector: 'bc-view-book-page',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<bc-selected-book-page></bc-selected-book-page>
`
})
export class ViewBookPageComponent implements OnDestroy {
actionsSubscription: Subscription; constructor(store: Store<fromRoot.State>, route: ActivatedRoute) {
this.actionsSubscription = route.params
.select<string>('id')
.map(id => new book.SelectAction(id))
.subscribe(store);
} ngOnDestroy() {
this.actionsSubscription.unsubscribe();
}
}
import 'rxjs/add/operator/let';
import { Observable } from 'rxjs/Observable';
import { Component, ChangeDetectionStrategy } from '@angular/core';
import { Store } from '@ngrx/store'; import * as fromRoot from '../reducers';
import * as layout from '../actions/layout'; @Component({
selector: 'bc-app',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<bc-layout>
<bc-sidenav [open]="showSidenav$ | async">
<bc-nav-item (activate)="closeSidenav()" routerLink="/" icon="book" hint="View your book collection">
My Collection
</bc-nav-item>
<bc-nav-item (activate)="closeSidenav()" routerLink="/book/find" icon="search" hint="Find your next book!">
Browse Books
</bc-nav-item>
</bc-sidenav>
<bc-toolbar (openMenu)="openSidenav()">
Book Collection
</bc-toolbar> <router-outlet></router-outlet>
</bc-layout>
`
})
export class AppComponent {
showSidenav$: Observable<boolean>; constructor(private store: Store<fromRoot.State>) {
/**
* Selectors can be applied with the `select` operator which passes the state
* tree to the provided selector
*/
this.showSidenav$ = this.store.select(fromRoot.getShowSidenav);
} closeSidenav() {
/**
* All state updates are handled through dispatched actions in 'container'
* components. This provides a clear, reproducible history of state
* updates and user interaction through the life of our
* application.
*/
this.store.dispatch(new layout.CloseSidenavAction());
} openSidenav() {
this.store.dispatch(new layout.OpenSidenavAction());
}
}
ngRx 官方示例分析 - 4.pages的更多相关文章
- ngRx 官方示例分析 - 3. reducers
上一篇:ngRx 官方示例分析 - 2. Action 管理 这里我们讨论 reducer. 如果你注意的话,会看到在不同的 Action 定义文件中,导出的 Action 类型名称都是 Action ...
- ngRx 官方示例分析 - 2. Action 管理
我们从 Action 名称开始. 解决 Action 名称冲突问题 在 ngRx 中,不同的 Action 需要一个 Action Type 进行区分,一般来说,这个 Action Type 是一个字 ...
- ngRx 官方示例分析 - 1. 介绍
ngRx 的官方示例演示了在具体的场景中,如何使用 ngRx 管理应用的状态. 示例介绍 示例允许用户通过查询 google 的 book API 来查询图书,并保存自己的精选书籍列表. 菜单有两 ...
- ngRx 官方示例分析 - 6 - Effect
@ngrx/effect 前面我们提到,在 Book 的 reducer 中,并没有 Search 这个 Action 的处理,由于它需要发出一个异步的请求,等到请求返回前端,我们需要根据返回的结果来 ...
- ngRx 官方示例分析 - 5. components
组件通过标准的 Input 和 Output 进行操作,并不直接访问 store. /app/components/book-authors.ts import { Component, Input ...
- RocketMQ源码分析之从官方示例窥探:RocketMQ事务消息实现基本思想
摘要: RocketMQ源码分析之从官方示例窥探RocketMQ事务消息实现基本思想. 在阅读本文前,若您对RocketMQ技术感兴趣,请加入RocketMQ技术交流群 RocketMQ4.3.0版本 ...
- Halcon斑点分析官方示例讲解
官方示例中有许多很好的例子可以帮助大家理解和学习Halcon,下面举几个经典的斑点分析例子讲解一下 Crystals 图中显示了在高层大气中采集到的晶体样本的图像.任务是分析对象以确定特定形状的频率. ...
- DotNetBar for Windows Forms 12.7.0.10_冰河之刃重打包版原创发布-带官方示例程序版
关于 DotNetBar for Windows Forms 12.7.0.10_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版------------- ...
- DotNetBar for Windows Forms 12.5.0.2_冰河之刃重打包版原创发布-带官方示例程序版
关于 DotNetBar for Windows Forms 12.5.0.2_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版-------------- ...
随机推荐
- GIT的使用中的问题处理
GIT 的常规操作 常规操作也是我自己平时常用的几个命令, 学自于 pro git 这本书中 git 配置文件 git的配置文件位置针对所有用户:/etc/gitconfig针对当前用户: -/.gi ...
- ubuntu环境下python虚拟环境的安装
一. 虚拟环境搭建 在开发中安装模块的方法: pip install 模块名称 之前我们安装模块都是直接在物理环境下安装,这种安装方法,后面一次安装的会覆盖掉前面一次安装的.那如果一台机器上面开发多个 ...
- JDK中AbstractQueuedSynchronizer应用解析
这个类首先是一个抽象类,定义了一个模板,很多java同步相关的类(ReetrantLock.Semaphore.CountDownLatch等)都是基于AbstractQueuedSynchroniz ...
- hbase (local mode) remote access
如果初学hbase,没必要弄一个集群,hbase的local mode够用了. 这里写一个简单的教程.适用于初学hbase,想要用代码访问hbase的童鞋们. 目录: 0. 准备 1). 开发环境 ...
- H5开发中的故障
本篇博文会不断的收录我在做H5页面时遇到的问题以及解决方案,当然有的问题,我也没有遇到好的解决方案,所以如果你有解决的办法,请务必不吝赐教! H5开发中的故障 微信APP返回按钮不刷新页面 ...
- 深入理解JVM(五)——垃圾回收器
轻松学习JVM(五)——垃圾回收器 上一篇我们介绍了常见的垃圾回收算法,不同的算法各有各的优缺点,在JVM中并不是单纯的使用某一种算法进行垃圾回收,而是将不同的垃圾回收算法包装在不同的垃圾回收器当中, ...
- VS2015配置内核WDK7600环境,32位下.
VS2015配置内核WDK7600环境,32位下. 学习内核驱动的编写,就要会配置环境.不然总是用记事本编写.比较不方便. 环境配置如下. 1.首先下载WDK7600, 课堂资料代码中已经上传.链接: ...
- VirtualBoX虚拟机里安装linux系统,在虚拟系统里安装增强功能报错解决方法
http://www.cnblogs.com/MoShin/archive/2012/04/25/2469156.html 当我们在虚拟机里安装lixunx系统,避免不了的要安装增强功能,无论是视觉效 ...
- windows 连接Linux
服务器:阿里云 ecs 从 Windows 环境远程登录 Linux 实例 远程登录软件的用法大同小异.本文档以 Putty 为例,介绍如何远程登录实例.Putty 操作简单.免费.免安装, 下载地址 ...
- 提取微信小程序“头脑王者”业务逻辑
产品经理今天让我整理微信小程序"头脑王者"的产品逻辑,花了一天时间在XMind写了写,整理后的内容如图,分享给大家希望大家可以多多点评,互相学习,不知道应该写什么,重要的内容都在图 ...