美文网首页
中间人模式

中间人模式

作者: 南蓝NL | 来源:发表于2017-11-25 22:04 被阅读0次
image.png

所谓中间人模式,就是两个组件在都不知道对方的条件下能够进行通信,它们通过一个中间人来进行传值,这里的中间人是父组件
场景:我们定义一个下单的组件和一个报价的组件,通过点击立即购买,将报价组件的值传到下单组件去
QuotePirce组件在这个报价里面主要是通过响应式编程将报价发射出去
quote-price.component.html

 <input type="button" value="立即购买" (click)="buyStock($event)">

quote-price.component.ts

@Output()   //发射东西出去
  lastPrice:EventEmitter<PriceQuote> = new EventEmitter(); //准备用它来发射事件
  @Output()
  buy:EventEmitter<PriceQuote> = new EventEmitter();

  constructor() { 
    setInterval(()=>{
      let priceQuote:PriceQuote = new PriceQuote(this.stcokCode,100*Math.random()); //使用随机函数来模拟股票价格的波动,两个参数一个是股票代码,一个是股票价格
       this.price = priceQuote.lastPrice;
       this.lastPrice.emit(priceQuote)
    },1000)
  }
  //把价格发射出去,不管谁去接受
  buyStock(event){
    this.buy.emit(new PriceQuote(this.stcokCode,this.price));
  }
  ngOnInit() {
  }
}
export class PriceQuote{  //PriceQuote是自定义的一个类
  constructor(public stockCode:string,
              public lastPrice:number
            ){

            }}

然后通过中间人来接受,这里的中间人是app组件
app.component.html

<!-- 通过事件绑定从报价组件接受 -->
<app-price-quote (buy)="buyHandler($event)"></app-price-quote> 
<!-- 通过属性绑定送到到订单组件 -->
<app-order [priceQuote]="priceQuote"></app-order>

app.component.ts

export class AppComponent {
  priceQuote:PriceQuote = new PriceQuote("",0);
  //拿到了报价组件发射过来的值;
  buyHandler(event:PriceQuote){
    this.priceQuote = event;
  }
}

然后在下单组件里面order.component.ts

 @Input()
   priceQuote:PriceQuote;

最后在order.component.html里面定义

<div>
 买100手{{priceQuote.stockCode}}手股票,买入价格是{{priceQuote.lastPrice|number:'2.2-2'}}
</div>
最终效果.png

相关文章

  • 中间人模式

    中间人模式,用来解决两个组件松耦合的数据传递。例如有A、B、C三个组件,A是B、C的父组件,那么A就可以设计成B、...

  • 中间人模式

    所谓中间人模式,就是两个组件在都不知道对方的条件下能够进行通信,它们通过一个中间人来进行传值,这里的中间人是父组件...

  • 3.中间人模式。

    子组件把报价传到父组件,然后父组件把报价传给另一个子组件(报价组件把报价传给父组件,然后父组件传给订单组件) 设计...

  • 一文搞懂ARP欺骗原理和DNS劫持原理(图文解释

    中间人攻击: ARP欺骗和DNS劫持都属于中间人攻击,所以首先需要了解什么是中间人攻击。 中间人攻击(Man-in...

  • 10.《Angular中间人模式》

    一、我们要实现的效果 1是根组件,当组件7想把消息传递给组件8时,我们这时就需要中间人组件3 如果你已经看了我的上...

  • 15 Vue- 中间人模式

    json

  • 《中间人经济》

    互联网时代还需要中间人吗? 中间人为什么依然大行其道? 什么情况下的中间人价值最大? 互联网的出现,会将中间人彻底...

  • MITM攻击(中间人攻击)

    关于HTTPS,我经常会提到的就是中间人攻击,那究竟什么是中间人攻击呢?中间人攻击,即所谓的Main-in-the...

  • MVP设计架构+生命周期封装防止内存泄漏

    1 MVP模式的三个角色1 .Persenter-交互的中间人主要作为View和model的桥梁,负责书写一些业...

  • 利用Jmitm对OpenSSH2.0版本进行中间人攻击中间人攻击

    利用Jmitm对OpenSSH2.0版本进行中间人攻击中间人攻击 准备三台linux虚拟机,除了中间人机器...

网友评论

      本文标题:中间人模式

      本文链接:https://www.haomeiwen.com/subject/ldkhbxtx.html