1
1
/* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { Inject , Injectable , Optional } from '@angular/core' ;
2
+ import { inject , Injectable } from '@angular/core' ;
3
+ import { SIGNAL , SignalNode } from '@angular/core/primitives/signals' ;
3
4
4
5
import { deepMergeKey } from '@delon/util/other' ;
5
6
import type { NzSafeAny } from 'ng-zorro-antd/core/types' ;
@@ -8,11 +9,7 @@ import { AlainConfig, AlainConfigKey, ALAIN_CONFIG } from './config.types';
8
9
9
10
@Injectable ( { providedIn : 'root' } )
10
11
export class AlainConfigService {
11
- private config : AlainConfig ;
12
-
13
- constructor ( @Optional ( ) @Inject ( ALAIN_CONFIG ) defaultConfig ?: AlainConfig ) {
14
- this . config = { ...defaultConfig } ;
15
- }
12
+ private readonly config = { ...inject ( ALAIN_CONFIG , { optional : true } ) } ;
16
13
17
14
get < T extends AlainConfigKey > ( componentName : T , key ?: string ) : AlainConfig [ T ] {
18
15
const res = ( ( this . config [ componentName ] as { [ key : string ] : unknown } ) || { } ) as NzSafeAny ;
@@ -23,12 +20,20 @@ export class AlainConfigService {
23
20
return deepMergeKey ( { } , true , ...defaultValues , this . get ( componentName ) ) ;
24
21
}
25
22
23
+ /**
24
+ * 将配置附加到当前实例中,支持 Signal 信号
25
+ */
26
26
attach < T extends AlainConfigKey > ( componentThis : unknown , componentName : T , defaultValues : AlainConfig [ T ] ) : void {
27
- Object . assign ( componentThis as any , this . merge ( componentName , defaultValues ) ) ;
28
- }
29
-
30
- attachKey < T extends AlainConfigKey > ( componentThis : unknown , componentName : T , key : string ) : void {
31
- Object . assign ( componentThis as any , this . get ( componentName , key ) ) ;
27
+ const data = this . merge < T > ( componentName , defaultValues ) ;
28
+ Object . entries ( data as Object ) . forEach ( ( [ key , value ] ) => {
29
+ const t = componentThis as any ;
30
+ const s = t [ key ] ?. [ SIGNAL ] as SignalNode < any > ;
31
+ if ( s != null ) {
32
+ s . value = value ;
33
+ } else {
34
+ t [ key ] = value ;
35
+ }
36
+ } ) ;
32
37
}
33
38
34
39
set < T extends AlainConfigKey > ( componentName : T , value : AlainConfig [ T ] ) : void {
0 commit comments