我正在尝试将旧的 Angular 组件库迁移为独立组件。
许多组件有很多共同点,因此我使用超类来表示所有常见行为。至少按照我的习惯, a 的超类@Component
不应该是另一个@Component
,而应该是@Directive
。
超类的开始如下所示:
// @dynamic
@Directive()
export abstract class DigitSequenceEditorDirective<T> implements
AfterViewInit, ControlValueAccessor, OnInit, OnDestroy, Validator {
// ...
其中一个子类组件的开头如下:
// @dynamic
@Component({
selector: 'tbw-time-editor',
animations: [BACKGROUND_ANIMATIONS],
templateUrl: '../digit-sequence-editor/digit-sequence-editor.directive.html',
styleUrls: ['../digit-sequence-editor/digit-sequence-editor.directive.scss', './time-editor.component.scss'],
providers: [{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => TimeEditorComponent), multi: true },
{ provide: NG_VALIDATORS, useExisting: forwardRef(() => TimeEditorComponent), multi: true }]
})
export class TimeEditorComponent extends DigitSequenceEditorDirective<number> implements OnInit {
// ...
现在的问题是,将其移植到 Angular 19 并尝试使用独立设计,指令的 HTML 包含一些 CommonModule 功能,如[ngClass]
和[ngStyle]
。(也有和之类的用法,但我可以使用、等*ngIf
摆脱它们。)@if
@for
Google AI 对在指令中使用功能的非功能性建议CommonModule
如下:
import { Directive, Input } from '@angular/core';
import { NgIf } from '@angular/common';
@Directive({
selector: '[appMyStandalone]',
standalone: true,
imports: [NgIf] // Can't be done! No `imports` field! (I'd have NgClass and NgStyle here if this worked)
})
export class MyStandaloneDirective {
@Input() appMyStandalone: boolean = false;
}
如果我不能使用imports
指令,我该如何解决下面的红色波浪线问题?