|
1 | 1 | import { isPlatformServer } from '@angular/common';
|
2 | 2 | import { Component, Inject, NgZone, OnInit, PLATFORM_ID } from '@angular/core';
|
3 |
| -import { BehaviorSubject } from 'rxjs'; |
| 3 | +import { rxState } from '@rx-angular/state'; |
| 4 | +import { of } from 'rxjs'; |
4 | 5 |
|
5 | 6 | @Component({
|
6 | 7 | selector: 'rx-angular-root',
|
7 | 8 | template: `
|
8 |
| - <div id="let" *rxLet="color$ as color; strategy">{{ color }}</div> |
| 9 | + <div id="let" *rxLet="color$; let color">{{ color }}</div> |
9 | 10 | <div id="push">{{ color$ | push }}</div>
|
10 | 11 | <div id="unpatch" [unpatch]="['click']" (click)="onClick()"></div>
|
11 | 12 | <div class="for" *rxFor="let color of colors$">{{ color }}</div>
|
12 | 13 | `,
|
13 | 14 | })
|
14 | 15 | export class AppComponent implements OnInit {
|
15 |
| - color$ = new BehaviorSubject('red'); |
16 |
| - colors$ = new BehaviorSubject(['red']); |
| 16 | + private readonly state = rxState<{ color: string; colors: string[] }>( |
| 17 | + ({ set, connect }) => { |
| 18 | + set('color', () => 'red'); |
| 19 | + connect('colors', of(['red'])); |
| 20 | + } |
| 21 | + ); |
| 22 | + |
| 23 | + readonly color$ = this.state.select('color'); |
| 24 | + readonly colors$ = this.state.select('colors'); |
17 | 25 |
|
18 | 26 | constructor(@Inject(PLATFORM_ID) private platformId: string) {}
|
19 | 27 |
|
20 | 28 | ngOnInit() {
|
21 | 29 | if (isPlatformServer(this.platformId)) {
|
22 |
| - this.color$.next('green'); |
23 |
| - this.colors$.next(['green', 'purple']); |
| 30 | + this.state.set('color', () => 'green'); |
| 31 | + this.state.set('colors', () => ['green', 'purple']); |
24 | 32 | }
|
25 | 33 | }
|
26 | 34 |
|
|
0 commit comments