Detailed explanation of the middleman mode of Angular components

Detailed explanation of the middleman mode of Angular components

1. Middleman Model

Except for component 1, each component in this component tree has a parent component that can play the role of a middleman. The top-level middleman is component 1, which enables components 2, 3, and 6 to communicate with each other. By analogy, component 2 is the middleman between component 4 and component 5. Component 3 is the middleman between components 7 and 8.

A middleman is responsible for receiving data from one component and passing it to another.

2. Examples

Taking the stock quote component as an example, suppose a trader is monitoring the price of the quote component. When the stock price reaches a certain value, the trader will click a buy button to buy the stock. Problem: The quote component does not know how to place an order to buy a stock, it is only used to monitor the stock price. Therefore, the quotation component should notify its middleman [that is, the APP component] at this time to tell it that the trader wants to buy a certain stock at a certain price. The middleman should know which component can complete the order and pass the stock code and current price to this component.

1. Add a buy button to the quotation component

Add a button to the quote component. When the stock reaches a certain price, the trader can click the button to buy the stock at this price.

<div>
  I am a quotation component</div>
<div>
  The stock code is {{stockCode}} and the stock price is {{price | number:"2.0-2"}}
</div>
<div>
  <input type="button" value="Buy Now" (click)="$($event)">
</div>
@Output()
  buy:EventEmitter<PriceQuote>=new EventEmitter();

  buyStock(event){
    this.buy.emit(new PriceQuote(this.stockCode,this.price));
  }

2. Parent component receives and processes events

Listen to the buy event in the parent component and obtain the current purchase information

<app-price-quote (buy)="buyHandler($event)" ></app-price-quote>
  buyHandler(event:PriceQuote){
    this.priceQuote=event;
  }

The quotation information can be passed to the order component through attribute binding.

<app-order [priceQuote]="priceQuote"></app-order>

3. Order component

The order component has an input property to receive the quote and display it on the page.

  @Input()
  priceQuote:PriceQuote;
<div>
  I am placing an order component</div>
<div>
  Buy 100 lots of {{priceQuote.stockCode}} stock at {{priceQuote.lastPrice | number:"2.2-2"}}
</div>

4. Operation results

The price of the quotation component is constantly changing. Clicking Buy Now will buy the current stock at the current price. It will be updated whenever you click the button.

Benefits: There is no code related to the order component in the quotation component, and the quotation component does not even know the existence of the order component. The quote component simply emits the stock symbol and stock price at the time of purchase. Likewise, there is nothing in the order component related to the quote component. The quotation component and the order component work together to complete the function of placing stock orders without knowing each other. High component reuse.

3. Using a service as a middleman

If two components do not have a common parent component and are not even displayed together, how can they communicate? For example, components 4 and 6 in the figure at the beginning of the article.

In this case, an injectable service should be used as a middleman. Whenever a component is created, the middleman service is injected. Components can subscribe to the stream of events emitted by a service.

Before developing an application with Angular, you should think deeply and design which reusable components to write, such as order components, quotation components, and which components and services act as middlemen for which components. What are the inputs of the components, what are the outputs, and how do the components communicate with each other. Then start writing code.

The above is a detailed explanation of the middleman mode of Angular components. For more information about Angular components, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of Angular parent-child component communication
  • Detailed explanation of Angular dynamic components
  • Angular performance optimization: third-party components and lazy loading technology
  • Example of implementing communication between angular components
  • Detailed explanation of the method of value transfer test between angular components
  • Angular7 creates projects, components, services and uses services
  • Angular events: how to pass data between different components
  • Angularjs1.5 Example of using functions to pass values ​​outside a component
  • Detailed explanation of Angular6 study notes: master-slave components

<<:  Detailed analysis of the parameter file my.cnf of MySQL in Ubuntu

>>:  Complete steps to build a Laravel development environment using Docker

Recommend

Detailed explanation of various usages of proxy_pass in nginx

Table of contents Proxy forwarding rules The firs...

How to set the page you are viewing to not allow Baidu to save its snapshot

Today, when I searched for a page on Baidu, becaus...

Web design dimensions and rules for advertising design on web pages

1. Under 800*600, if the width of the web page is...

Front-end state management (Part 1)

Table of contents 1. What is front-end state mana...

Several magical uses of JS ES6 spread operator

Table of contents 1. Add attributes 2. Merge mult...

Causes and solutions for front-end exception 502 bad gateway

Table of contents 502 bad gateway error formation...

Detailed explanation of 4 common data sources in Spark SQL

Generic load/write methods Manually specify optio...

JavaScript implements the pot-beating game of Gray Wolf

1. Project Documents 2. Use HTML and CSS for page...

Detailed steps for building Portainer visual interface with Docker

In order to solve the problem mentioned last time...

mysql data insert, update and delete details

Table of contents 1. Insert 2. Update 3. Delete 1...

JavaScript setTimeout and setTimeinterval use cases explained

Both methods can be used to execute a piece of ja...

Solution to mysql ERROR 1045 (28000) problem

I encountered mysql ERROR 1045 and spent a long t...

js, css, html determine the various versions of the browser

Use regular expressions to determine the IE browse...

How to dynamically modify container port mapping in Docker

Preface: Docker port mapping is often done by map...

Let's talk about what JavaScript's URL object is

Table of contents Overview Hash Properties Host p...