Component Architecture

Our previous CounterComponent example is called a smart component - it knew about Redux, the structure of the state and the actions itneeded to call. In theory you can drop this component into any area of your application and just let it work, but it will be tightly bound to that specific slice of state and those specific actions.

For example, what if we wanted to have multiple counters tracking different things on the page? Or how about counting the number of red clicks vs blue clicks?

To help make components more generic and reusable, it's worth trying to separatethem into container components and presentational components.

Container Components Presentational Components
Location
——-
Top level, route handlers Middle and leaf components
Aware of Redux
——-
Yes No
To read data
——-
Subscribe to Redux state Read state from @Input properties
To change data
——-
Dispatch Redux actions Invoke callbacks from @Output properties

redux docs

Keeping this in mind, let's refactor our CounterComponent to be a presentational component.

Modifying AppComponent to become a smart component

First, let's modify our top-level application component to use the CounterService and CounterActions, just as CounterComponent did:

app/app.component.ts

  1. import {Component} from '@angular/core';
  2. import {Observable} from 'rxjs';
  3. import {Counter} from '../../models/counter';
  4. import {CounterService} from '../../services/counter.service';
  5. import {CounterActions} from '../../store/counter/counter.actions';
  6. @Component({
  7. selector: 'app-root',
  8. templateUrl: './app.component.html',
  9. styleUrls: ['./app.component.css']
  10. })
  11. export class AppComponent {
  12. counter$: Observable<Counter>;
  13. constructor(
  14. counterService: CounterService,
  15. public actions: CounterActions
  16. ) {
  17. this.counter$ = counterService.getCounter();
  18. }
  19. }

Now our AppComponent is a smart-component, because it's aware of Redux, it's presence in the application state and the underlying services. As with previous examples, we can use the async pipe to obtain the most recent counter value and pass it along to other components within the template.

And while we haven't looked at the @Output()'s on CounterComponent just yet, we'll want to delegate those events to our action creators in CounterActions.

app/app.component.html

  1. <counter [counter]="counter$ | async"
  2. (onIncrement)="actions.increment()"
  3. (onDecrement)="actions.decrement()"
  4. (onReset)="actions.reset()">
  5. </counter>

Modifying CounterComponent to become a presentation component

In turn, we need to make the CounterComponent from a smart component into a dumb component. For this, we will pass the data into the component using @Input properties and click events using @Output() properties, removing the use of CounterService and CounterActions entirely.

app/counter/counter.component.ts

  1. import {Component, Input, EventEmitter, Output} from '@angular/core';
  2. import {Counter} from '../../models/counter';
  3. @Component({
  4. selector: 'counter',
  5. templateUrl: './counter.component.html'
  6. })
  7. export class CounterComponent {
  8. @Input()
  9. counter: Counter;
  10. @Output()
  11. onIncrement: EventEmitter<void> = new EventEmitter<void>();
  12. @Output()
  13. onDecrement: EventEmitter<void> = new EventEmitter<void>();
  14. @Output()
  15. onReset: EventEmitter<void> = new EventEmitter<void>();
  16. }

Our child components become much simpler and testable, because we don't have to use the async pipe to work with our state, which removes a lot of pain when dealing with lots of @Input's or the need to use complex expressions with Observable's.

We can also now simply use core Angular features to emit values whenever a click event happens:

app/counter/counter.component.html

  1. <p>
  2. Clicked: {{counter.currentValue}} times
  3. <button (click)="onIncrement.emit()">+</button>
  4. <button (click)="onDecrement.emit()">-</button>
  5. <button (click)="onReset.emit()">Reset</button>
  6. </p>

We now have a nicely-reusable presentational component with no knowledge ofRedux or our application state.

原文: https://angular-2-training-book.rangle.io/handout/state-management/ngrx/component_architecture.html