簡體   English   中英

如何在 Angular 中注釋擴展組件 class? (遺產)

[英]How to annotate an extended component class in Angular? (Inheritance)

我有很多子組件Child1Component等,如何為擴展父 class ParentComponent編寫注釋?

/** Parent Component */
/** <----- How to annotate here? */
export class ParentComponent implements OnInit {
  constructor(i: INJECTED1, otherParam: string) {
    ...
  }
  ...
}

/** Child Component */
@Component({selector: ..., template: ..., styleUrls: ...})
export class Child1Component extends ParentComponent {
  constructor(i: INJECTED1, j: INJECTED2) {
    super(i, 'otherParam');
  }
  ...
}

到目前為止我嘗試過的(成功有限):

  • 使用@Directive()注釋ParentComponent是迄今為止我發現的最接近的,因為 angular 編譯器似乎以所需的方式處理它。 但是,這感覺不對,因為這顯然不是 angular 指令。 linter 對此也不滿意,期望*.directive.ts以文件名結尾而無需進一步調整。
  • 根本沒有注釋ParentComponent 這會導致 Angular 編譯器拋出錯誤NG2007: Class is using Angular features but is not decorated
  • 使用@Component({template: ''})注釋ParentComponent感覺好多了,並且讓 linter 對文件名感到滿意。 但是 angular 編譯現在堅持ParentComponent的所有參數都是可注入的,但事實並非如此,因為它們被超級 class 調用。

是的,使用@Directive 進行注釋是通往 go 的方法(至少從 angular 9 開始,如果我沒記錯的話,早期版本的 angular 不需要)。

您可以將 ParentComponent 重命名為 ParentDirective 並相應地重命名文件以使 linter 滿意,或者您可以將 linter 配置為忽略這種情況。

Edit: I prefer renaming ParentComponent to ParentDirective, because when you think about it a component is basically a directive (ie a selector that makes Angular treat certain html tags or html directive) but components have a template and styles while directives don't. 由於您的父母 class 沒有模板,因此將其命名為指令是有意義的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM