|
1 | | -import { Component } from '@angular/core' |
2 | | -import { AsyncPipe, CommonModule } from '@angular/common' |
3 | | -import { BehaviorSubject, map, Observable } from 'rxjs' |
| 1 | +import { Component, OnDestroy } from '@angular/core' |
| 2 | +import { CommonModule, AsyncPipe } from '@angular/common' |
| 3 | +import { BehaviorSubject, Subscription } from 'rxjs' |
| 4 | +import { CounterComponent } from './counter' |
| 5 | +import { LabelComponent } from './label' |
4 | 6 |
|
5 | 7 | @Component({ |
6 | 8 | selector: 'app-root', |
7 | 9 | standalone: true, |
8 | | - imports: [CommonModule, AsyncPipe], |
9 | | - templateUrl: './app.html', |
10 | | - styleUrls: ['./app.css'] |
| 10 | + imports: [CommonModule, AsyncPipe, CounterComponent, LabelComponent], |
| 11 | + templateUrl: './app.html' |
11 | 12 | }) |
12 | | -export class App { |
13 | | - private readonly countSubject = new BehaviorSubject<number>(0) |
| 13 | +export class App implements OnDestroy { |
| 14 | + counter = new BehaviorSubject<number>(0) |
| 15 | + counter$ = this.counter.asObservable() |
| 16 | + private sub: Subscription |
14 | 17 |
|
15 | | - readonly count$: Observable<number> = this.countSubject.asObservable() |
16 | | - readonly double$: Observable<number> = this.count$.pipe(map(v => v * 2)) |
| 18 | + constructor() { |
| 19 | + this.sub = this.counter$.subscribe(v => console.log('Subscriber A:', v)) |
| 20 | + } |
17 | 21 |
|
18 | | - inc(): void { |
19 | | - this.countSubject.next(this.countSubject.value + 10) |
| 22 | + inc() { |
| 23 | + this.counter.next(this.counter.value + 1) |
20 | 24 | } |
21 | 25 |
|
22 | | - dec(): void { |
23 | | - this.countSubject.next(this.countSubject.value - 10) |
| 26 | + dec() { |
| 27 | + this.counter.next(this.counter.value - 1) |
24 | 28 | } |
25 | 29 |
|
26 | | - reset(): void { |
27 | | - this.countSubject.next(0) |
| 30 | + ngOnDestroy() { |
| 31 | + this.sub.unsubscribe() |
28 | 32 | } |
29 | 33 | } |
30 | 34 |
|
31 | 35 |
|
32 | 36 |
|
33 | | -// import { Component } from '@angular/core' |
34 | | -// import { BehaviorSubject, map, Observable } from 'rxjs' |
35 | | - |
36 | | -// @Component({ |
37 | | -// selector: 'app-root', |
38 | | -// templateUrl: './app.html', |
39 | | -// styleUrls: ['./app.css'] |
40 | | -// }) |
41 | | -// export class App { |
42 | | -// private readonly countSubject = new BehaviorSubject<number>(0) |
43 | | - |
44 | | -// readonly count$: Observable<number> = this.countSubject.asObservable() |
45 | | -// readonly double$: Observable<number> = this.count$.pipe(map(v => v * 2)) |
46 | | - |
47 | | -// inc(): void { |
48 | | -// this.countSubject.next(this.countSubject.value + 10) |
49 | | -// } |
50 | | - |
51 | | -// dec(): void { |
52 | | -// this.countSubject.next(this.countSubject.value - 10) |
53 | | -// } |
54 | | - |
55 | | -// reset(): void { |
56 | | -// this.countSubject.next(0) |
57 | | -// } |
58 | | -// } |
59 | | - |
60 | | - |
61 | | - |
62 | | -// import { Component } from '@angular/core'; |
63 | | -// import { BehaviorSubject, map } from 'rxjs'; |
| 37 | +// import { Component, OnDestroy } from '@angular/core' |
| 38 | +// import { CommonModule, AsyncPipe } from '@angular/common' |
| 39 | +// import { BehaviorSubject, Subscription } from 'rxjs' |
| 40 | +// import { CounterComponent } from './counter' |
| 41 | +// import { LabelComponent } from './label' |
64 | 42 |
|
65 | 43 | // @Component({ |
66 | 44 | // selector: 'app-root', |
67 | | -// templateUrl: './app.html', |
68 | | -// styleUrls: ['./app.css'] |
| 45 | +// standalone: true, |
| 46 | +// imports: [CommonModule, AsyncPipe, CounterComponent, LabelComponent], |
| 47 | +// templateUrl: './app.html' |
69 | 48 | // }) |
70 | | -// export class AppComponent { |
71 | | -// count$ = new BehaviorSubject<number>(0); |
72 | | -// double$ = this.count$.pipe(map(v => v * 2)); |
73 | | - |
74 | | -// inc() { |
75 | | -// this.count$.next(this.count$.value + 10); |
76 | | -// } |
| 49 | +// export class App implements OnDestroy { |
| 50 | +// counter = new BehaviorSubject<number>(0) |
| 51 | +// counter$ = this.counter.asObservable() |
| 52 | +// private sub: Subscription = this.counter$.subscribe(v => console.log('Subscriber A:', v)) |
77 | 53 |
|
78 | | -// dec() { |
79 | | -// this.count$.next(this.count$.value - 10); |
80 | | -// } |
| 54 | +// inc() { this.counter.next(this.counter.value + 1) } |
| 55 | +// dec() { this.counter.next(this.counter.value - 1) } |
81 | 56 |
|
82 | | -// reset() { |
83 | | -// this.count$.next(0); |
84 | | -// } |
85 | | - |
86 | | -// constructor() { |
87 | | -// console.log('00000000001:', this.count$); |
88 | | -// this.double$.subscribe(); |
89 | | -// } |
| 57 | +// ngOnDestroy() { this.sub.unsubscribe() } |
90 | 58 | // } |
91 | | - |
0 commit comments