From f57dc577a300ae98f6140337ce3c1de9ddb392cb Mon Sep 17 00:00:00 2001 From: cipchk Date: Fri, 19 Jan 2024 16:14:22 +0800 Subject: [PATCH] chore: clean constructor --- packages/abc/media/media.service.ts | 10 ++++---- packages/acl/src/acl-guard.ts | 8 +++---- packages/acl/src/acl-if.directive.ts | 21 +++++++++------- packages/acl/src/acl.directive.ts | 22 ++++++++++------- packages/auth/src/social/social.service.ts | 14 +++++------ .../src/store/cookie-storage.service.spec.ts | 24 +++++++++++++------ .../auth/src/store/cookie-storage.service.ts | 4 +++- packages/auth/src/token/jwt/jwt.guard.ts | 6 ++--- .../auth/src/token/simple/simple.guard.ts | 6 ++--- packages/auth/src/token/token.service.ts | 12 ++++------ packages/cache/src/cache.service.ts | 17 +++++++------ .../cache/src/local-storage-cache.service.ts | 4 ++-- packages/chart/card/card.component.ts | 6 ++--- .../chart/chart-echarts/echarts.component.ts | 14 +++++------ .../chart/chart-echarts/echarts.service.ts | 9 ++++--- packages/chart/core/g2.base.component.ts | 17 ++++++------- packages/chart/core/g2.servicce.ts | 10 ++++---- .../mini-progress/mini-progress.component.ts | 9 ++++--- .../chart/water-wave/water-wave.component.ts | 19 +++++++-------- packages/form/src/sf-fixed.directive.ts | 23 +++++++++++------- packages/form/src/sf-item-wrap.component.ts | 6 ++--- packages/form/src/sf-item.component.ts | 11 ++++----- packages/form/src/widget.factory.ts | 4 ++-- 23 files changed, 146 insertions(+), 130 deletions(-) diff --git a/packages/abc/media/media.service.ts b/packages/abc/media/media.service.ts index 6fedd1e7ca..5316ce9587 100644 --- a/packages/abc/media/media.service.ts +++ b/packages/abc/media/media.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { Observable, Subject, share } from 'rxjs'; import { AlainConfigService, AlainMediaConfig } from '@delon/util/config'; @@ -6,6 +6,9 @@ import { LazyService } from '@delon/util/other'; @Injectable({ providedIn: 'root' }) export class MediaService { + private readonly cogSrv = inject(AlainConfigService); + private readonly lazySrv = inject(LazyService); + private _cog!: AlainMediaConfig; private loading = false; private loaded = false; @@ -24,11 +27,6 @@ export class MediaService { )!; } - constructor( - private cogSrv: AlainConfigService, - private lazySrv: LazyService - ) {} - load(): this { if (this.loading) { if (this.loaded) { diff --git a/packages/acl/src/acl-guard.ts b/packages/acl/src/acl-guard.ts index d178f942fd..35970221bc 100644 --- a/packages/acl/src/acl-guard.ts +++ b/packages/acl/src/acl-guard.ts @@ -7,11 +7,9 @@ import type { ACLCanType, ACLGuardData } from './acl.type'; @Injectable({ providedIn: 'root' }) export class ACLGuardService { - constructor( - private srv: ACLService, - private router: Router, - private injector: Injector - ) {} + private readonly srv = inject(ACLService); + private readonly router = inject(Router); + private readonly injector = inject(Injector); process(data?: ACLGuardData): Observable { data = { diff --git a/packages/acl/src/acl-if.directive.ts b/packages/acl/src/acl-if.directive.ts index 6b3e2753d9..04db9f550d 100644 --- a/packages/acl/src/acl-if.directive.ts +++ b/packages/acl/src/acl-if.directive.ts @@ -1,4 +1,5 @@ -import { Directive, EmbeddedViewRef, Input, OnDestroy, TemplateRef, ViewContainerRef } from '@angular/core'; +import { Directive, EmbeddedViewRef, Input, OnDestroy, TemplateRef, ViewContainerRef, inject } from '@angular/core'; +import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { Subscription, filter } from 'rxjs'; import { ACLService } from './acl.service'; @@ -10,23 +11,25 @@ import { ACLCanType } from './acl.type'; standalone: true }) export class ACLIfDirective implements OnDestroy { + private readonly srv = inject(ACLService); + private readonly _viewContainer = inject(ViewContainerRef); static ngAcceptInputType_except: boolean | string | undefined | null; private _value!: ACLCanType; private _change$: Subscription; - private _thenTemplateRef: TemplateRef | null = null; + private _thenTemplateRef: TemplateRef | null = inject(TemplateRef); private _elseTemplateRef: TemplateRef | null = null; private _thenViewRef: EmbeddedViewRef | null = null; private _elseViewRef: EmbeddedViewRef | null = null; private _except = false; - constructor( - templateRef: TemplateRef, - private srv: ACLService, - private _viewContainer: ViewContainerRef - ) { - this._change$ = this.srv.change.pipe(filter(r => r != null)).subscribe(() => this._updateView()); - this._thenTemplateRef = templateRef; + constructor() { + this._change$ = this.srv.change + .pipe( + takeUntilDestroyed(), + filter(r => r != null) + ) + .subscribe(() => this._updateView()); } @Input() diff --git a/packages/acl/src/acl.directive.ts b/packages/acl/src/acl.directive.ts index 12d11d55b9..c85a9900cb 100644 --- a/packages/acl/src/acl.directive.ts +++ b/packages/acl/src/acl.directive.ts @@ -1,4 +1,5 @@ -import { Directive, ElementRef, Input, OnDestroy, Renderer2 } from '@angular/core'; +import { Directive, ElementRef, Input, OnDestroy, Renderer2, inject } from '@angular/core'; +import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; import { Subscription, filter } from 'rxjs'; import { ACLService } from './acl.service'; @@ -10,6 +11,10 @@ import { ACLCanType } from './acl.type'; standalone: true }) export class ACLDirective implements OnDestroy { + private readonly el: HTMLElement = inject(ElementRef).nativeElement; + private readonly renderer = inject(Renderer2); + private readonly srv = inject(ACLService); + private _value!: ACLCanType; private change$: Subscription; @@ -26,7 +31,7 @@ export class ACLDirective implements OnDestroy { private set(value: ACLCanType): void { this._value = value; const CLS = 'acl__hide'; - const el = this.el.nativeElement; + const el = this.el; if (this.srv.can(this._value)) { this.renderer.removeClass(el, CLS); } else { @@ -34,12 +39,13 @@ export class ACLDirective implements OnDestroy { } } - constructor( - private el: ElementRef, - private renderer: Renderer2, - protected srv: ACLService - ) { - this.change$ = this.srv.change.pipe(filter(r => r != null)).subscribe(() => this.set(this._value)); + constructor() { + this.change$ = this.srv.change + .pipe( + takeUntilDestroyed(), + filter(r => r != null) + ) + .subscribe(() => this.set(this._value)); } ngOnDestroy(): void { diff --git a/packages/auth/src/social/social.service.ts b/packages/auth/src/social/social.service.ts index 1683e4228b..ffaa93d34c 100644 --- a/packages/auth/src/social/social.service.ts +++ b/packages/auth/src/social/social.service.ts @@ -1,10 +1,10 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { DOCUMENT } from '@angular/common'; -import { Inject, Injectable, OnDestroy } from '@angular/core'; +import { Injectable, OnDestroy, inject } from '@angular/core'; import { Router } from '@angular/router'; import { Observable, Observer } from 'rxjs'; -import { DA_SERVICE_TOKEN, ITokenModel, ITokenService } from '../token/interface'; +import { DA_SERVICE_TOKEN, ITokenModel } from '../token/interface'; const OPENTYPE = '_delonAuthSocialType'; const HREFCALLBACK = '_delonAuthSocialCallbackByHref'; @@ -13,16 +13,14 @@ export type SocialOpenType = 'href' | 'window'; @Injectable() export class SocialService implements OnDestroy { + private readonly tokenService = inject(DA_SERVICE_TOKEN); + private readonly doc = inject(DOCUMENT); + private readonly router = inject(Router); + private _win: Window | null = null; private _winTime: any; private observer!: Observer; - constructor( - @Inject(DA_SERVICE_TOKEN) private tokenService: ITokenService, - @Inject(DOCUMENT) private doc: any, - private router: Router - ) {} - /** * 使用窗体打开授权页,返回值是 `Observable` 用于订阅授权后返回的结果 * diff --git a/packages/auth/src/store/cookie-storage.service.spec.ts b/packages/auth/src/store/cookie-storage.service.spec.ts index 840d8dd0f6..7f890d092b 100644 --- a/packages/auth/src/store/cookie-storage.service.spec.ts +++ b/packages/auth/src/store/cookie-storage.service.spec.ts @@ -1,4 +1,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ +import { TestBed } from '@angular/core/testing'; + import { CookieOptions, CookieService } from '@delon/util/browser'; import { CookieStorageStore } from './cookie-storage.service'; @@ -16,13 +18,21 @@ describe('auth: cookie-storage', () => { beforeEach(() => { data = {}; putSpy = jasmine.createSpy('put').and.callFake((key: string, value: string) => (data[key] = value)); - store = new CookieStorageStore({ - put: putSpy, - get: jasmine.createSpy('get').and.callFake((key: string) => data[key]), - remove: jasmine.createSpy('remove').and.callFake((key: string) => { - delete data[key]; - }) - } as unknown as CookieService); + TestBed.configureTestingModule({ + providers: [ + { + provide: CookieService, + useValue: { + put: putSpy, + get: jasmine.createSpy('get').and.callFake((key: string) => data[key]), + remove: jasmine.createSpy('remove').and.callFake((key: string) => { + delete data[key]; + }) + } + } + ] + }); + store = new CookieStorageStore(); }); it('should be never return null', () => { diff --git a/packages/auth/src/store/cookie-storage.service.ts b/packages/auth/src/store/cookie-storage.service.ts index dc2f920a4e..842eea6629 100644 --- a/packages/auth/src/store/cookie-storage.service.ts +++ b/packages/auth/src/store/cookie-storage.service.ts @@ -1,3 +1,5 @@ +import { inject } from '@angular/core'; + import { CookieService } from '@delon/util/browser'; import { IStore } from './interface'; @@ -11,7 +13,7 @@ import { ITokenModel } from '../token/interface'; * ``` */ export class CookieStorageStore implements IStore { - constructor(private srv: CookieService) {} + private readonly srv = inject(CookieService); get(key: string): ITokenModel { try { diff --git a/packages/auth/src/token/jwt/jwt.guard.ts b/packages/auth/src/token/jwt/jwt.guard.ts index 963b69c4b5..5ae1adfa5a 100644 --- a/packages/auth/src/token/jwt/jwt.guard.ts +++ b/packages/auth/src/token/jwt/jwt.guard.ts @@ -1,13 +1,13 @@ -import { Inject, Injectable, inject } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { CanActivateChildFn, CanActivateFn, CanMatchFn } from '@angular/router'; import { JWTTokenModel } from './jwt.model'; import { CheckJwt, ToLogin } from '../helper'; -import { DA_SERVICE_TOKEN, ITokenService } from '../interface'; +import { DA_SERVICE_TOKEN } from '../interface'; @Injectable({ providedIn: 'root' }) export class AuthJWTGuardService { - constructor(@Inject(DA_SERVICE_TOKEN) private srv: ITokenService) {} + private readonly srv = inject(DA_SERVICE_TOKEN); process(url?: string): boolean { const cog = this.srv.options; diff --git a/packages/auth/src/token/simple/simple.guard.ts b/packages/auth/src/token/simple/simple.guard.ts index 7786968d85..ac2ef6ee80 100644 --- a/packages/auth/src/token/simple/simple.guard.ts +++ b/packages/auth/src/token/simple/simple.guard.ts @@ -1,13 +1,13 @@ -import { Inject, Injectable, inject } from '@angular/core'; +import { Injectable, inject } from '@angular/core'; import { CanActivateChildFn, CanActivateFn, CanMatchFn } from '@angular/router'; import { SimpleTokenModel } from './simple.model'; import { CheckSimple, ToLogin } from '../helper'; -import { DA_SERVICE_TOKEN, ITokenService } from '../interface'; +import { DA_SERVICE_TOKEN } from '../interface'; @Injectable({ providedIn: 'root' }) export class AuthSimpleGuardService { - constructor(@Inject(DA_SERVICE_TOKEN) private srv: ITokenService) {} + private readonly srv = inject(DA_SERVICE_TOKEN); process(url?: string): boolean { const res = CheckSimple(this.srv.get() as SimpleTokenModel); diff --git a/packages/auth/src/token/token.service.ts b/packages/auth/src/token/token.service.ts index 8f6cad619a..dfa24f59b6 100644 --- a/packages/auth/src/token/token.service.ts +++ b/packages/auth/src/token/token.service.ts @@ -1,14 +1,14 @@ -import { inject, Inject, Injectable, OnDestroy } from '@angular/core'; +import { inject, Injectable, OnDestroy } from '@angular/core'; import { BehaviorSubject, interval, Observable, Subject, Subscription, filter, map, share } from 'rxjs'; import { AlainAuthConfig, AlainConfigService } from '@delon/util/config'; import { AuthReferrer, ITokenModel, ITokenService } from './interface'; import { mergeConfig } from '../auth.config'; -import { DA_STORE_TOKEN, IStore } from '../store/interface'; +import { DA_STORE_TOKEN } from '../store/interface'; export function DA_SERVICE_TOKEN_FACTORY(): ITokenService { - return new TokenService(inject(AlainConfigService), inject(DA_STORE_TOKEN)); + return new TokenService(inject(AlainConfigService)); } /** @@ -16,16 +16,14 @@ export function DA_SERVICE_TOKEN_FACTORY(): ITokenService { */ @Injectable() export class TokenService implements ITokenService, OnDestroy { + private readonly store = inject(DA_STORE_TOKEN); private refresh$ = new Subject(); private change$ = new BehaviorSubject(null); private interval$?: Subscription; private _referrer: AuthReferrer = {}; private _options: AlainAuthConfig; - constructor( - configSrv: AlainConfigService, - @Inject(DA_STORE_TOKEN) private store: IStore - ) { + constructor(configSrv: AlainConfigService) { this._options = mergeConfig(configSrv); } diff --git a/packages/cache/src/cache.service.ts b/packages/cache/src/cache.service.ts index b4d1e218b4..ed3bd59b76 100644 --- a/packages/cache/src/cache.service.ts +++ b/packages/cache/src/cache.service.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { Platform } from '@angular/cdk/platform'; import { HttpClient } from '@angular/common/http'; -import { Inject, Injectable, OnDestroy } from '@angular/core'; +import { Injectable, OnDestroy, inject } from '@angular/core'; import { BehaviorSubject, Observable, of, map, tap } from 'rxjs'; import { addSeconds } from 'date-fns'; @@ -9,11 +9,15 @@ import { addSeconds } from 'date-fns'; import { AlainCacheConfig, AlainConfigService } from '@delon/util/config'; import { deepGet } from '@delon/util/other'; -import { CacheNotifyResult, CacheNotifyType, ICache, ICacheStore } from './interface'; +import { CacheNotifyResult, CacheNotifyType, ICache } from './interface'; import { DC_STORE_STORAGE_TOKEN } from './local-storage-cache.service'; @Injectable({ providedIn: 'root' }) export class CacheService implements OnDestroy { + private readonly store = inject(DC_STORE_STORAGE_TOKEN); + private readonly http = inject(HttpClient); + private readonly platform = inject(Platform); + private readonly memory: Map = new Map(); private readonly notifyBuffer: Map> = new Map< string, @@ -24,19 +28,14 @@ export class CacheService implements OnDestroy { private freqTime: any; private cog: AlainCacheConfig; - constructor( - cogSrv: AlainConfigService, - @Inject(DC_STORE_STORAGE_TOKEN) private store: ICacheStore, - private http: HttpClient, - private platform: Platform - ) { + constructor(cogSrv: AlainConfigService) { this.cog = cogSrv.merge('cache', { mode: 'promise', reName: '', prefix: '', meta_key: '__cache_meta' })!; - if (!platform.isBrowser) return; + if (!this.platform.isBrowser) return; this.loadMeta(); this.startExpireNotify(); } diff --git a/packages/cache/src/local-storage-cache.service.ts b/packages/cache/src/local-storage-cache.service.ts index 622bfae62b..bb0293614d 100644 --- a/packages/cache/src/local-storage-cache.service.ts +++ b/packages/cache/src/local-storage-cache.service.ts @@ -5,11 +5,11 @@ import { ICache, ICacheStore } from './interface'; export const DC_STORE_STORAGE_TOKEN = new InjectionToken('DC_STORE_STORAGE_TOKEN', { providedIn: 'root', - factory: () => new LocalStorageCacheService(inject(Platform)) + factory: () => new LocalStorageCacheService() }); export class LocalStorageCacheService implements ICacheStore { - constructor(private platform: Platform) {} + private readonly platform = inject(Platform); get(key: string): ICache | null { if (!this.platform.isBrowser) { diff --git a/packages/chart/card/card.component.ts b/packages/chart/card/card.component.ts index 62a1fb8a05..1c06bd01f7 100644 --- a/packages/chart/card/card.component.ts +++ b/packages/chart/card/card.component.ts @@ -6,7 +6,8 @@ import { OnChanges, TemplateRef, ViewEncapsulation, - booleanAttribute + booleanAttribute, + inject } from '@angular/core'; import { NzCardComponent } from 'ng-zorro-antd/card'; @@ -25,6 +26,7 @@ import { NzSpinComponent } from 'ng-zorro-antd/spin'; imports: [NzCardComponent, NzSpinComponent, NzStringTemplateOutletDirective] }) export class G2CardComponent implements OnChanges { + private readonly cdr = inject(ChangeDetectorRef); /** 是否显示边框 */ @Input({ transform: booleanAttribute }) bordered = false; @Input() avatar?: string | TemplateRef | null; @@ -42,8 +44,6 @@ export class G2CardComponent implements OnChanges { /** 是否显示Loading */ @Input({ transform: booleanAttribute }) loading = false; - constructor(private cdr: ChangeDetectorRef) {} - ngOnChanges(): void { this.cdr.detectChanges(); } diff --git a/packages/chart/chart-echarts/echarts.component.ts b/packages/chart/chart-echarts/echarts.component.ts index 27ca9f059e..a9ae2632c9 100644 --- a/packages/chart/chart-echarts/echarts.component.ts +++ b/packages/chart/chart-echarts/echarts.component.ts @@ -55,6 +55,11 @@ export class ChartEChartsComponent implements OnInit, OnDestroy { static ngAcceptInputType_width: NumberInput; static ngAcceptInputType_height: NumberInput; + private readonly srv = inject(ChartEChartsService); + private readonly cdr = inject(ChangeDetectorRef); + private readonly ngZone = inject(NgZone); + private readonly platform = inject(Platform); + @ViewChild('container', { static: true }) private node!: ElementRef; private destroy$ = inject(DestroyRef); private _chart: ChartECharts | null = null; @@ -106,12 +111,7 @@ export class ChartEChartsComponent implements OnInit, OnDestroy { } loaded = false; - constructor( - private srv: ChartEChartsService, - private cdr: ChangeDetectorRef, - private ngZone: NgZone, - private platform: Platform - ) { + constructor() { this.srv.notify .pipe( takeUntilDestroyed(), @@ -119,7 +119,7 @@ export class ChartEChartsComponent implements OnInit, OnDestroy { ) .subscribe(() => this.load()); - this.theme = srv.cog.echartsTheme; + this.theme = this.srv.cog.echartsTheme; } private emit(type: ChartEChartsEventType, other?: ChartEChartsEvent): void { diff --git a/packages/chart/chart-echarts/echarts.service.ts b/packages/chart/chart-echarts/echarts.service.ts index 029c78b210..1c6f1bc6a5 100644 --- a/packages/chart/chart-echarts/echarts.service.ts +++ b/packages/chart/chart-echarts/echarts.service.ts @@ -1,4 +1,4 @@ -import { Injectable, OnDestroy } from '@angular/core'; +import { Injectable, OnDestroy, inject } from '@angular/core'; import { Observable, Subject } from 'rxjs'; import { AlainChartConfig, AlainConfigService } from '@delon/util/config'; @@ -6,6 +6,8 @@ import { LazyService } from '@delon/util/other'; @Injectable({ providedIn: 'root' }) export class ChartEChartsService implements OnDestroy { + private readonly cogSrv = inject(AlainConfigService); + private readonly lazySrv = inject(LazyService); private _cog!: AlainChartConfig; private loading = false; private loaded = false; @@ -25,10 +27,7 @@ export class ChartEChartsService implements OnDestroy { )!; } - constructor( - private cogSrv: AlainConfigService, - private lazySrv: LazyService - ) { + constructor() { this.cog = { theme: '' }; } diff --git a/packages/chart/core/g2.base.component.ts b/packages/chart/core/g2.base.component.ts index b6903419cd..955c6b8368 100644 --- a/packages/chart/core/g2.base.component.ts +++ b/packages/chart/core/g2.base.component.ts @@ -13,6 +13,7 @@ import { SimpleChanges, ViewChild, booleanAttribute, + inject, numberAttribute } from '@angular/core'; import { Subject, Subscription, filter, takeUntil } from 'rxjs'; @@ -26,6 +27,12 @@ import { G2Service } from './g2.servicce'; @Directive() export abstract class G2BaseComponent implements OnInit, OnChanges, OnDestroy { + protected readonly srv = inject(G2Service); + protected readonly el: ElementRef = inject(ElementRef); + protected readonly ngZone = inject(NgZone); + protected readonly platform = inject(Platform); + protected readonly cdr = inject(ChangeDetectorRef); + get chart(): Chart { return this._chart; } @@ -34,14 +41,8 @@ export abstract class G2BaseComponent implements OnInit, OnChanges, OnDestroy { return (window as NzSafeAny).G2; } - constructor( - protected srv: G2Service, - protected el: ElementRef, - protected ngZone: NgZone, - protected platform: Platform, - protected cdr: ChangeDetectorRef - ) { - this.theme = srv.cog.theme!; + constructor() { + this.theme = this.srv.cog.theme!; this.srv.notify .pipe( takeUntil(this.destroy$), diff --git a/packages/chart/core/g2.servicce.ts b/packages/chart/core/g2.servicce.ts index 8189d89c32..39f50f550c 100644 --- a/packages/chart/core/g2.servicce.ts +++ b/packages/chart/core/g2.servicce.ts @@ -1,4 +1,4 @@ -import { Injectable, OnDestroy } from '@angular/core'; +import { Injectable, OnDestroy, inject } from '@angular/core'; import { Observable, Subject } from 'rxjs'; import { AlainChartConfig, AlainConfigService } from '@delon/util/config'; @@ -6,6 +6,9 @@ import { LazyService } from '@delon/util/other'; @Injectable({ providedIn: 'root' }) export class G2Service implements OnDestroy { + private readonly cogSrv = inject(AlainConfigService); + private readonly lazySrv = inject(LazyService); + private _cog!: AlainChartConfig; private loading = false; private loaded = false; @@ -28,10 +31,7 @@ export class G2Service implements OnDestroy { )!; } - constructor( - private cogSrv: AlainConfigService, - private lazySrv: LazyService - ) { + constructor() { this.cog = { theme: '' }; } diff --git a/packages/chart/mini-progress/mini-progress.component.ts b/packages/chart/mini-progress/mini-progress.component.ts index d13a2c3eea..26c4f29dc0 100644 --- a/packages/chart/mini-progress/mini-progress.component.ts +++ b/packages/chart/mini-progress/mini-progress.component.ts @@ -6,6 +6,7 @@ import { Input, OnChanges, ViewEncapsulation, + inject, numberAttribute } from '@angular/core'; @@ -24,16 +25,14 @@ import { NzTooltipDirective } from 'ng-zorro-antd/tooltip'; imports: [NzTooltipDirective, NgStyle] }) export class G2MiniProgressComponent implements OnChanges { + readonly i18n = inject(DelonLocaleService); + private readonly cdr = inject(ChangeDetectorRef); + @Input() color = '#1890FF'; @Input({ transform: numberAttribute }) target?: number | null; @Input({ transform: numberAttribute }) percent?: number | null; @Input({ transform: numberAttribute }) strokeWidth?: number | null; - constructor( - public i18n: DelonLocaleService, - private cdr: ChangeDetectorRef - ) {} - private fixNum(value: number | undefined | null): number { return Math.min(Math.max(numberAttribute(value), 0), 100); } diff --git a/packages/chart/water-wave/water-wave.component.ts b/packages/chart/water-wave/water-wave.component.ts index 97925d718e..8dec42e5fc 100644 --- a/packages/chart/water-wave/water-wave.component.ts +++ b/packages/chart/water-wave/water-wave.component.ts @@ -15,6 +15,7 @@ import { ViewChild, ViewEncapsulation, booleanAttribute, + inject, numberAttribute } from '@angular/core'; import { fromEvent, Subscription, debounceTime } from 'rxjs'; @@ -33,6 +34,12 @@ import { NzStringTemplateOutletDirective } from 'ng-zorro-antd/core/outlet'; imports: [NgStyle, NzStringTemplateOutletDirective] }) export class G2WaterWaveComponent implements OnDestroy, OnChanges, OnInit { + private readonly el: HTMLElement = inject(ElementRef).nativeElement; + private readonly renderer = inject(Renderer2); + private readonly ngZone = inject(NgZone); + private readonly cdr = inject(ChangeDetectorRef); + private readonly platform = inject(Platform); + private resize$: Subscription | null = null; @ViewChild('container', { static: true }) private node!: ElementRef; private timer!: number; @@ -48,14 +55,6 @@ export class G2WaterWaveComponent implements OnDestroy, OnChanges, OnInit { // #endregion - constructor( - private el: ElementRef, - private renderer: Renderer2, - private ngZone: NgZone, - private cdr: ChangeDetectorRef, - private platform: Platform - ) {} - private renderChart(isUpdate: boolean): void { if (!this.resize$) return; @@ -203,9 +202,9 @@ export class G2WaterWaveComponent implements OnDestroy, OnChanges, OnInit { } private updateRadio(): void { - const { offsetWidth } = this.el.nativeElement.parentNode; + const { offsetWidth } = this.el.parentNode! as HTMLElement; const radio = offsetWidth < this.height ? offsetWidth / this.height : 1; - this.renderer.setStyle(this.el.nativeElement, 'transform', `scale(${radio})`); + this.renderer.setStyle(this.el, 'transform', `scale(${radio})`); } render(): void { diff --git a/packages/form/src/sf-fixed.directive.ts b/packages/form/src/sf-fixed.directive.ts index e9299b1357..c84ed44077 100644 --- a/packages/form/src/sf-fixed.directive.ts +++ b/packages/form/src/sf-fixed.directive.ts @@ -1,14 +1,26 @@ -import { AfterViewInit, Directive, ElementRef, Input, OnChanges, Renderer2, numberAttribute } from '@angular/core'; +import { + AfterViewInit, + Directive, + ElementRef, + Input, + OnChanges, + Renderer2, + inject, + numberAttribute +} from '@angular/core'; @Directive({ selector: '[fixed-label]' }) export class SFFixedDirective implements AfterViewInit, OnChanges { + private readonly el: HTMLElement = inject(ElementRef).nativeElement; + private readonly render = inject(Renderer2); + private _inited = false; - @Input({ alias: 'fixed-label', transform: numberAttribute }) num?: number | null; + @Input({ alias: 'fixed-label', transform: (v: unknown) => (v == null ? 0 : numberAttribute(v)) }) num?: number | null; private init(): void { if (!this._inited || this.num == null || this.num <= 0) return; - const el = this.el.nativeElement; + const el = this.el; const widgetEl = el.querySelector('.ant-row') || el; this.render.addClass(widgetEl, 'sf__fixed'); const labelEl = widgetEl.querySelector('.ant-form-item-label'); @@ -22,11 +34,6 @@ export class SFFixedDirective implements AfterViewInit, OnChanges { } } - constructor( - private el: ElementRef, - private render: Renderer2 - ) {} - ngAfterViewInit(): void { this._inited = true; this.init(); diff --git a/packages/form/src/sf-item-wrap.component.ts b/packages/form/src/sf-item-wrap.component.ts index 1bda78eab3..2df27c7877 100644 --- a/packages/form/src/sf-item-wrap.component.ts +++ b/packages/form/src/sf-item-wrap.component.ts @@ -1,4 +1,4 @@ -import { Component, Input, OnChanges, ViewEncapsulation } from '@angular/core'; +import { Component, Input, OnChanges, ViewEncapsulation, inject } from '@angular/core'; import { helpMotion } from 'ng-zorro-antd/core/animation'; import { NzFormStatusService } from 'ng-zorro-antd/core/form'; @@ -14,6 +14,8 @@ import type { SFOptionalHelp, SFUISchemaItem } from './schema/ui'; encapsulation: ViewEncapsulation.None }) export class SFItemWrapComponent implements OnChanges { + private readonly statusSrv = inject(NzFormStatusService); + _showTitle: boolean = false; @Input() id?: string; @Input() schema!: SFSchema; @@ -34,8 +36,6 @@ export class SFItemWrapComponent implements OnChanges { return this.ui.optionalHelp as SFOptionalHelp; } - constructor(private statusSrv: NzFormStatusService) {} - ngOnChanges(): void { const hasError = !!this.error; this.statusSrv.formStatusChanges.next({ status: hasError ? 'error' : '', hasFeedback: !!this.ui.feedback }); diff --git a/packages/form/src/sf-item.component.ts b/packages/form/src/sf-item.component.ts index 3c8e9bd433..3c5ee58f87 100644 --- a/packages/form/src/sf-item.component.ts +++ b/packages/form/src/sf-item.component.ts @@ -8,7 +8,8 @@ import { TemplateRef, ViewChild, ViewContainerRef, - ViewEncapsulation + ViewEncapsulation, + inject } from '@angular/core'; import { Subject } from 'rxjs'; @@ -35,6 +36,9 @@ let nextUniqueId = 0; providers: [NzFormStatusService] }) export class SFItemComponent implements OnInit, OnChanges, OnDestroy { + private readonly widgetFactory = inject(WidgetFactory); + private readonly terminator = inject(TerminatorService); + private ref!: ComponentRef>; readonly destroy$ = new Subject(); widget: Widget | null = null; @@ -45,11 +49,6 @@ export class SFItemComponent implements OnInit, OnChanges, OnDestroy { @ViewChild('target', { read: ViewContainerRef, static: true }) private container!: ViewContainerRef; - constructor( - private widgetFactory: WidgetFactory, - private terminator: TerminatorService - ) {} - onWidgetInstanciated(widget: Widget): void { this.widget = widget; const id = `_sf-${nextUniqueId++}`; diff --git a/packages/form/src/widget.factory.ts b/packages/form/src/widget.factory.ts index 6af8478b15..cb8c9b42a8 100644 --- a/packages/form/src/widget.factory.ts +++ b/packages/form/src/widget.factory.ts @@ -1,4 +1,4 @@ -import { ComponentRef, Injectable, ViewContainerRef } from '@angular/core'; +import { ComponentRef, Injectable, ViewContainerRef, inject } from '@angular/core'; import type { NzSafeAny } from 'ng-zorro-antd/core/types'; @@ -37,7 +37,7 @@ export class WidgetRegistry { @Injectable() export class WidgetFactory { - constructor(private registry: WidgetRegistry) {} + private readonly registry = inject(WidgetRegistry); createWidget(container: ViewContainerRef, type: string): ComponentRef> { if (!this.registry.has(type)) {