组件通过标准的 Input 和 Output 进行操作,并不直接访问 store.

/app/components/book-authors.ts

import { Component, Input } from '@angular/core';

import { Book } from '../models/book';

@Component({
selector: 'bc-book-authors',
template: `
<h5 md-subheader>Written By:</h5>
<span>
{{ authors | bcAddCommas }}
</span>
`,
styles: [`
h5 {
margin-bottom: 5px;
}
`]
})
export class BookAuthorsComponent {
@Input() book: Book; get authors() {
return this.book.volumeInfo.authors;
}
}

/app/components/book-detail.ts

import { Component, Input, Output, EventEmitter } from '@angular/core';
import { Book } from '../models/book'; @Component({
selector: 'bc-book-detail',
template: `
<md-card *ngIf="book">
<md-card-title-group>
<md-card-title>{{ title }}</md-card-title>
<md-card-subtitle *ngIf="subtitle">{{ subtitle }}</md-card-subtitle>
<img md-card-sm-image *ngIf="thumbnail" [src]="thumbnail"/>
</md-card-title-group>
<md-card-content>
<p [innerHtml]="description"></p>
</md-card-content>
<md-card-footer class="footer">
<bc-book-authors [book]="book"></bc-book-authors>
</md-card-footer>
<md-card-actions align="start">
<button md-raised-button color="warn" *ngIf="inCollection" (click)="remove.emit(book)">
Remove Book from Collection
</button> <button md-raised-button color="primary" *ngIf="!inCollection" (click)="add.emit(book)">
Add Book to Collection
</button>
</md-card-actions>
</md-card> `,
styles: [`
:host {
display: flex;
justify-content: center;
margin: 75px 0;
}
md-card {
max-width: 600px;
}
md-card-title-group {
margin-left: 0;
}
img {
width: 60px;
min-width: 60px;
margin-left: 5px;
}
md-card-content {
margin: 15px 0 50px;
}
md-card-actions {
margin: 25px 0 0 !important;
}
md-card-footer {
padding: 0 25px 25px;
position: relative;
}
`]
})
export class BookDetailComponent {
/**
* Presentational components receieve data through @Input() and communicate events
* through @Output() but generally maintain no internal state of their
* own. All decisions are delegated to 'container', or 'smart'
* components before data updates flow back down.
*
* More on 'smart' and 'presentational' components: https://gist.github.com/btroncone/a6e4347326749f938510#utilizing-container-components
*/
@Input() book: Book;
@Input() inCollection: boolean;
@Output() add = new EventEmitter<Book>();
@Output() remove = new EventEmitter<Book>(); /**
* Tip: Utilize getters to keep templates clean
*/
get id() {
return this.book.id;
} get title() {
return this.book.volumeInfo.title;
} get subtitle() {
return this.book.volumeInfo.subtitle;
} get description() {
return this.book.volumeInfo.description;
} get thumbnail() {
return this.book.volumeInfo.imageLinks.smallThumbnail;
}
}

ngRx 官方示例分析 - 5. components的更多相关文章

  1. ngRx 官方示例分析 - 3. reducers

    上一篇:ngRx 官方示例分析 - 2. Action 管理 这里我们讨论 reducer. 如果你注意的话,会看到在不同的 Action 定义文件中,导出的 Action 类型名称都是 Action ...

  2. ngRx 官方示例分析 - 2. Action 管理

    我们从 Action 名称开始. 解决 Action 名称冲突问题 在 ngRx 中,不同的 Action 需要一个 Action Type 进行区分,一般来说,这个 Action Type 是一个字 ...

  3. ngRx 官方示例分析 - 1. 介绍

    ngRx 的官方示例演示了在具体的场景中,如何使用 ngRx 管理应用的状态. 示例介绍 示例允许用户通过查询 google 的 book  API  来查询图书,并保存自己的精选书籍列表. 菜单有两 ...

  4. ngRx 官方示例分析 - 4.pages

    Page 中通过构造函数注入 Store,基于 Store 进行数据操作. 注意 Component 使用了 changeDetection: ChangeDetectionStrategy.OnPu ...

  5. ngRx 官方示例分析 - 6 - Effect

    @ngrx/effect 前面我们提到,在 Book 的 reducer 中,并没有 Search 这个 Action 的处理,由于它需要发出一个异步的请求,等到请求返回前端,我们需要根据返回的结果来 ...

  6. Halcon斑点分析官方示例讲解

    官方示例中有许多很好的例子可以帮助大家理解和学习Halcon,下面举几个经典的斑点分析例子讲解一下 Crystals 图中显示了在高层大气中采集到的晶体样本的图像.任务是分析对象以确定特定形状的频率. ...

  7. RocketMQ源码分析之从官方示例窥探:RocketMQ事务消息实现基本思想

    摘要: RocketMQ源码分析之从官方示例窥探RocketMQ事务消息实现基本思想. 在阅读本文前,若您对RocketMQ技术感兴趣,请加入RocketMQ技术交流群 RocketMQ4.3.0版本 ...

  8. DotNetBar for Windows Forms 12.7.0.10_冰河之刃重打包版原创发布-带官方示例程序版

    关于 DotNetBar for Windows Forms 12.7.0.10_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版------------- ...

  9. DotNetBar for Windows Forms 12.5.0.2_冰河之刃重打包版原创发布-带官方示例程序版

    关于 DotNetBar for Windows Forms 12.5.0.2_冰河之刃重打包版 --------------------11.8.0.8_冰河之刃重打包版-------------- ...

随机推荐

  1. Swift语言中与C/C++和Java不同的语法(四)

    这一节,我们将会讨论一下Swift中的函数相关的基本内容 首先是函数的创建: func sayHello (name:String) -> String { return "Hello ...

  2. Head First设计模式之中介者模式

    一.定义 又称为调停者模式,定义一个中介对象来封装系列对象之间的交互.中介者使各个对象不需要显示地相互引用,从而使其耦合性松散,而且可以独立地改变他们之间的交互. 二.结构 组成: ● 抽象中介者(M ...

  3. XML的解析(DOM以及SAX方式)

    感谢http://blog.csdn.net/redarmy_chen/article/details/12951649(关于SAX解析)以及http://blog.csdn.net/zhangerq ...

  4. vue cli搭建项目及文件引入

    cli搭建方法:需安装nodejs先 1.npm install -g cnpm --registry=https://registry.npm.taobao.org //安装cnpm,用cnpm下载 ...

  5. Robot Framework学习笔记(九)------创建资源和用户关键字

    一.测试套件下创建用户关键字 1.创建关键字测试套件右击->点击new user keyword,然后输入name,点击OK保存. 2.在用户关键字的edit点击settings,然后输入Arg ...

  6. 20 Zabbix系统性能优化建议

    点击返回:自学Zabbix之路 20 Zabbix系统性能优化建议 1. Zabbix性能变慢的可能表现: zabbix队列有太多被延迟的item,可以通过administration-queue查看 ...

  7. 机器学习 | 从加法模型讲到GBDT算法

    作者:JSong, 日期:2017.10.10 集成学习(ensemble learning)通过构建并结合多个学习器来完成学习任务,常可获得比单一学习器显著优越的泛化性能,这对"弱学习器& ...

  8. java web 之 listen 与 filter

    一.Listener监听器 Javaweb开发中的监听器,是用于监听web常见对象 HttpServletRequest HttpSession ServletContext 监听它们的创建与销毁.属 ...

  9. 优雅的处理Redis访问超时

    很长一段时间以来,一直在项目中使用Redis作为辅助存储,确切来说是利用Redis的内存存储,而不是将其作为缓存.比如常见的利用Set集合来判断某个数值是否存在,或者将来自不同请求的数据放在Redis ...

  10. C#操作MongoDB的简单实例

    最近比较忙,很久没更新了(虽然没人看,也没人在乎,也要记得be yourself), 前面分享了一些mongodb的安装和简单的语法,今天模仿支付宝首页的模块移动功能,用mongo做一个简单的后台实例 ...