ngRx 官方示例分析 - 5. components
组件通过标准的 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的更多相关文章
- 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 官方示例分析 - 4.pages
		
Page 中通过构造函数注入 Store,基于 Store 进行数据操作. 注意 Component 使用了 changeDetection: ChangeDetectionStrategy.OnPu ...
 - ngRx 官方示例分析 - 6 - Effect
		
@ngrx/effect 前面我们提到,在 Book 的 reducer 中,并没有 Search 这个 Action 的处理,由于它需要发出一个异步的请求,等到请求返回前端,我们需要根据返回的结果来 ...
 - Halcon斑点分析官方示例讲解
		
官方示例中有许多很好的例子可以帮助大家理解和学习Halcon,下面举几个经典的斑点分析例子讲解一下 Crystals 图中显示了在高层大气中采集到的晶体样本的图像.任务是分析对象以确定特定形状的频率. ...
 - RocketMQ源码分析之从官方示例窥探:RocketMQ事务消息实现基本思想
		
摘要: RocketMQ源码分析之从官方示例窥探RocketMQ事务消息实现基本思想. 在阅读本文前,若您对RocketMQ技术感兴趣,请加入RocketMQ技术交流群 RocketMQ4.3.0版本 ...
 - 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_冰河之刃重打包版-------------- ...
 
随机推荐
- JavaScript的DOM编程--12--innerHTML属性
			
innerHTML属性: 1). 浏览器几乎都支持该属性, 但不是 DOM 标准的组成部分. innerHTML 属性可以用来读, 写某给定元素里的 HTML 内容 <html> < ...
 - TurnipBit—MicroPython开发板:从积木式编程语言开始学做小小创客
			
编程.建模.制作动画和游戏--这些当初我们默认只有成年人玩得转的事情,现在早已经被无数小孩子给颠覆甚至玩出新境界了.热爱科技和动手的"创客"(Maker)现在在全世界都炙手可热.今 ...
 - angular4.0使用sass
			
一.为什么选择sass为了更好的管理代码,我们需要可以定义变量,同时可以使用函数的样式工具,比如定义公用color.bg.显然css无法满足需求.现在市面上常用的是sass.less.经过比较,我选择 ...
 - Hadoop2.9.0安装
			
参考 https://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-common/SingleCluster.html 1.下载并解 ...
 - jquery/Js属性无效
			
今天遇到个很奇葩的问题,就是checkbox的onchange时间无效,我一共写了两个checkbox的onchange事件,但就是只有一个能用,本来我以为是jquery的兼容问题,但是换成js还是不 ...
 - 【网络流】POJ1273 Drainage Ditches
			
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 78671 Accepted: 3068 ...
 - vim 简单笔记
			
vim编辑器Linux系统常用的一种编辑器 有三种模式 命令模式:插入模式:编辑模式 1 插入模式的基本操作: 从命令模式切入到插入模式只需要注意有三个字母aio就可以了 a是在当前光标后插入 ...
 - Chris Richardson微服务翻译:微服务之事件驱动的数据管理
			
Chris Richardson 微服务系列翻译全7篇链接: 微服务介绍 构建微服务之使用API网关 构建微服务之微服务架构的进程通讯 微服务架构中的服务发现 微服务之事件驱动的数据管理(本文) 微服 ...
 - 自定义注解,andjdk提供的元注解
			
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface FruitN ...
 - IdentityServer4 中文文档
			
一.介绍 特性一览 整体介绍 术语的解释 支持的规范 包和构建说明 二.快速入门 设置和概述 #1 使用客户端证书控制API访问 #2 使用密码认证方式控制API访问 #3 使用OpenId Conn ...