File tree Expand file tree Collapse file tree 1 file changed +0
-53
lines changed
projects/www/src/app/pages/guide/signals Expand file tree Collapse file tree 1 file changed +0
-53
lines changed Original file line number Diff line number Diff line change 26
26
27
27
To define a class-based SignalStore, create a new class and extend from `signalStore`.
28
28
29
- <ngrx-code-example >
30
-
31
- ``` ts
32
- @Injectable ()
33
- export class CounterStore extends signalStore (
34
- { protectedState: false },
35
- withState ({ count: 0 })
36
- ) {
37
- readonly doubleCount = computed (() => this .count () * 2 );
38
-
39
- increment(): void {
40
- patchState (this , { count: this .count () + 1 });
41
- }
42
- }
43
- ```
44
-
45
- </ngrx-code-example >
46
-
47
29
</details >
48
30
49
31
<details >
50
32
<summary ><b >#4</b > How can I get the type of a SignalStore?</summary >
51
33
52
34
To get the type of a SignalStore, use the `InstanceType` utility type.
53
35
54
- <ngrx-code-example >
55
-
56
- ``` ts
57
- const CounterStore = signalStore (withState ({ count: 0 }));
58
-
59
- type CounterStore = InstanceType <typeof CounterStore >;
60
-
61
- function logCount(store : CounterStore ): void {
62
- console .log (store .count ());
63
- }
64
- ```
65
-
66
- </ngrx-code-example >
67
-
68
36
</details >
69
37
70
38
<details >
71
39
<summary ><b >#5</b > Can I inject a SignalStore via the constructor?</summary >
72
40
73
41
Yes. To inject a SignalStore via the constructor, define and export its type with the same name.
74
42
75
- <ngrx-code-example >
76
-
77
- ``` ts
78
- // counter-store.ts
79
- export const CounterStore = signalStore (withState ({ count: 0 }));
80
-
81
- export type CounterStore = InstanceType <typeof CounterStore >;
82
-
83
- // counter.ts
84
- import { CounterStore } from ' ./counter.store' ;
85
-
86
- @Component ({
87
- /* ... */
88
- })
89
- export class Counter {
90
- constructor (readonly store : CounterStore ) {}
91
- }
92
- ```
93
-
94
- </ngrx-code-example >
95
-
96
43
</details >
You can’t perform that action at this time.
0 commit comments