一、输入属性(父组件与子组件通信)

1. 创建工程

ng new demo1

2.创建order组件

ng g component order

3. 在order组件里定义输入属性

order组件的html

4. 父组件

app.component.ts中定义stock

app.component.html, 采用双向绑定

效果图

最终父组件IBM的值,通过输入属性,把值传递给了子组件

二、输出属性(子组件与父组件通信)

1. ng g component priceQutoe 创建报价组件

2. 定义报价组件控制器

  1. import {Component, EventEmitter, OnInit, Output} from '@angular/core';
  2.  
  3. @Component({
  4. selector: 'app-price-quote',
  5. templateUrl: './price-quote.component.html',
  6. styleUrls: ['./price-quote.component.css']
  7. })
  8. export class PriceQuoteComponent implements OnInit {
  9.  
  10. stockCode: string = 'IBM';
  11. price: number;
  12.  
  13. @Output('priceChange')
  14. lastPrice: EventEmitter<PriceQuote> = new EventEmitter();
  15.  
  16. constructor() {
  17. setInterval(() => {
  18. let priceQuote: PriceQuote = new PriceQuote(this.stockCode, 100 * Math.random());
  19. this.price = priceQuote.lastPrice;
  20. this.lastPrice.emit(priceQuote);
  21. }, 1000);
  22. }
  23.  
  24. ngOnInit() {
  25. }
  26.  
  27. }
  28.  
  29. export class PriceQuote {
  30. constructor(public stockCode: string,
  31. public lastPrice: number) {
  32. }
  33. }

 3. 定义报价组件html

  1. <p>
  2. 这里是报价组件
  3. </p>
  4. <div>
  5. 股票代码是{{stockCode}}, 股票价格是{{price | number:'2.2-2'}}
  6. </div>

  

4. 父组件控制器

  1. import { Component } from '@angular/core';
  2. import {PriceQuote} from './price-quote/price-quote.component';
  3.  
  4. @Component({
  5. selector: 'app-root',
  6. templateUrl: './app.component.html',
  7. styleUrls: ['./app.component.css']
  8. })
  9. export class AppComponent {
  10.  
  11. stock = '';
  12. title = 'app';
  13.  
  14. priceQuote: PriceQuote = new PriceQuote('', 0);
  15.  
  16. priceQutoehandler(event: PriceQuote){
  17. this.priceQuote = event;
  18. }
  19. }

  

5. 父组件html

  1. <div>
  2. 我是父组件
  3. </div>
  4. <app-price-quote (priceChange)="priceQutoehandler($event)"></app-price-quote>
  5. <div>
  6. 这是在报价组件外部,股票代码是{{priceQuote.stockCode}},
  7. 股票价格是{{priceQuote.lastPrice | number:'2.2-2'}}
  8. </div>

6.效果图

三、中间人模式

当另个组件不是父子组件关系时,需要两个共同的父组件,这个父组件就是中间人模式

中间人模式同时使用了输入属性和输出属性

1. 报价组件定义

  1. import {Component, EventEmitter, OnInit, Output} from '@angular/core';
  2.  
  3. @Component({
  4. selector: 'app-price-quote',
  5. templateUrl: './price-quote.component.html',
  6. styleUrls: ['./price-quote.component.css']
  7. })
  8. export class PriceQuoteComponent implements OnInit {
  9.  
  10. stockCode: string = 'IBM';
  11. price: number;
  12.  
  13. //@Output('priceChange')
  14. //lastPrice: EventEmitter<PriceQuote> = new EventEmitter()
  15.  
  16. @Output()
  17. buy: EventEmitter<PriceQuote> = new EventEmitter();
  18.  
  19. constructor() {
  20. setInterval(() => {
  21. const priceQuote: PriceQuote = new PriceQuote(this.stockCode, 100 * Math.random());
  22. this.price = priceQuote.lastPrice;
  23. //this.lastPrice.emit(priceQuote);
  24. }, 1000);
  25. }
  26.  
  27. buyStock(event) {
  28. this.buy.emit(new PriceQuote(this.stockCode, this.price));
  29. }
  30.  
  31. ngOnInit() {
  32. }
  33.  
  34. }
  35.  
  36. export class PriceQuote {
  37. constructor(public stockCode: string,
  38. public lastPrice: number) {
  39. }
  40. }

  

2. 报价组件html

  1. <p>
  2. 这里是报价组件
  3. </p>
  4. <div>
  5. 股票代码是{{stockCode}}, 股票价格是{{price | number:'2.2-2'}}
  6. </div>
  7. <div>
  8. <input type='button' value='立即购买' (click)="buyStock($event)">
  9. </div>

  

3.订单组件控制器

  1. import {Component, Input, OnInit} from '@angular/core';
  2. import {PriceQuote} from "../price-quote/price-quote.component";
  3.  
  4. @Component({
  5. selector: 'app-order',
  6. templateUrl: './order.component.html',
  7. styleUrls: ['./order.component.css']
  8. })
  9. export class OrderComponent implements OnInit {
  10.  
  11. @Input()
  12. priceQutoe: PriceQuote;
  13.  
  14. constructor() { }
  15.  
  16. ngOnInit() {
  17. }
  18.  
  19. }

  

4. 订单组件html

  1. <div>
  2. 我下单组件
  3. </div>
  4. <div>
  5. 买100手{{priceQutoe.stockCode}}股票,买入价为{{priceQutoe.lastPrice | number:'2.2-2'}}
  6. </div>

  

5. 父组件的控制器

  1. import { Component } from '@angular/core';
  2. import {PriceQuote} from './price-quote/price-quote.component';
  3.  
  4. @Component({
  5. selector: 'app-root',
  6. templateUrl: './app.component.html',
  7. styleUrls: ['./app.component.css']
  8. })
  9. export class AppComponent {
  10.  
  11. stock = '';
  12.  
  13. priceQuote: PriceQuote = new PriceQuote('', 0);
  14.  
  15. priceQutoehandler(event: PriceQuote){
  16. this.priceQuote = event;
  17. }
  18.  
  19. buyHandler(event: PriceQuote) {
  20. this.priceQuote = event;
  21. }
  22. }

  

6.父组件的html

  1. <div>
  2. 我是父组件
  3. </div>
  4. <app-price-quote (buy)="buyHandler($event)"></app-price-quote>
  5. <app-order [priceQutoe]="priceQuote"></app-order>

  

7.效果图

当点击“立即购买”时,显示当时的显示价格。

Angular 4 组件间的通信的更多相关文章

  1. angular组件间的通信(父子、不同组件的数据、方法的传递和调用)

    angular组件间的通信(父子.不同组件的数据.方法的传递和调用) 一.不同组件的传值(使用服务解决) 1.创建服务组件 不同组件相互传递,使用服务组件,比较方便,简单,容易.先将公共组件写在服务的 ...

  2. 第四节:Vue表单标签和组件的基本用法,父子组件间的通信

    vue表单标签和组件的基本用法,父子组件间的通信,直接看例子吧. <!DOCTYPE html> <html> <head> <meta charset=&q ...

  3. ZeroMQ(java)之I/O线程的实现与组件间的通信

    算是开始读ZeroMQ(java)的代码实现了吧,现在有了一个大体的了解,看起来实现是比较的干净的,抽象什么的不算复杂... 这里先来看看它的I/O线程的实现吧,顺带看看是如何实现组件的通信的.... ...

  4. vuejs单一事件管理组件间的通信

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. React中父子组件间的通信问题

    1.https://blog.csdn.net/sinat_17775997/article/details/59103173 (React中父子组件间的通信问题)

  6. Vue2不使用Vuex如何实现兄弟组件间的通信

    在一些正规的大型项目的企业级开发过程中我们一般会引入Vuex来对Vue所有组件进行状态管理,可以轻松实现各组件间的通信.但是有时候做做自己的小项目,没有必要使用Vuex时,如何简单的实现组件间的通信? ...

  7. (尚033)Vue_案例_slot(组件间的通信4:slot)

    1.组件间的通信4:slot(slot:插槽,就是一个占位) slot用于标签反复使用很多次 1.1理解 此方式用于父组件向子组件传递标签数据, 其他为数据通信 外面组件向里面组件传递标签进去,直接拿 ...

  8. 后端学 Angular 2 —— 组件间通信

    1. 父组件向子组件传递信息 使用@Input 子组件的属性用 @Input 进行修饰,在父组件的模板中绑定变量 例子: import { Component, OnInit, Input } fro ...

  9. vue组件间的通信

    组件的定义: 组件(Component)是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素, Vue.js 的编译器为它添加特殊功能.v ...

随机推荐

  1. java容器——Collection接口

    Collection是Set,List接口的父类接口,用于存储集合类型的数据. 2.方法 int size():返回集合的长度 void clear():清除集合里的所有元素,将集合长度变为0 Ite ...

  2. idea中使用gradle

    idea中使用gradle gradle下载 gradle下载地址:https://services.gradle.org/distributions/ 这里假设下载的是4.6版本的,如下: 笔者下载 ...

  3. consumer的DubboResponseTimeoutScanTimer线程

    考虑这样一种情况,由于网络延时,consumer先抛出超时异常,一段时间后又收到了已经超时的响应,dubbo是怎么处理的? 拆分为3步看: 1. consumer的DubboResponseTimeo ...

  4. win10解除密码

  5. js运动例子1

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta name ...

  6. SQL Server SqlCacheDependency 缓存依赖

     SQL server数据缓存依赖有两种实现模式,轮询模式,通知模式. 1  轮询模式实现步骤 此模式需要SQL SERVER 7.0/2000/2005版本以上版本都支持 主要包含以下几步:  1. ...

  7. 太过亲密往往不好——用non-member,non-friend替换member函数

    在前一篇文章,我们提到,使用private来代替public以提高class的封装性.这一篇文章,我们将对接口发起攻势.首先来个简单的例子. class WebBrowser { public: vo ...

  8. 读书笔记 C# Linq查询之group关键字浅析

    在C#中,自从有了Linq查询表达式后,程序员对可被迭代的序列或列表执行一系列的筛选.排序.过滤.分组.查询等操作.本文章所要讲述的是group关键字. Linq查询表达式,是以from关键字开头,以 ...

  9. python学习:变量与字符串

    counter = # 赋值整型变量 miles = 1000.0 # 浮点型 name = "John" # 字符串 print counter print miles prin ...

  10. header 输出原始的php报头文件

    header() 函数向客户端发送原始的 HTTP 报头. 认识到一点很重要,即必须在任何实际的输出被发送之前调用 header() 函数 (在 PHP 4 以及更高的版本中,您可以使用输出缓存来解决 ...