[英]The selector of the component “AppFooterComponent” should be used as element
我的應用程序的結構如下:
<body>
<header>[...]</header>
<main>[...]</main>
<footer><app-footer></app-footer></footer>
</body>
由於我不需要不必要的DOM元素,因此我更喜歡將app-footer
聲明為屬性,以便可以這樣聲明頁面:
<body>
<header>[...]</header>
<main>[...]</main>
<footer app-footer></footer>
</body>
但是如果這樣做,執行ng lint
時會收到以下錯誤消息
組件“ AppFooterComponent”的選擇器應用作元素( https://angular.io/styleguide#style-05-03 )
我認為這種情況是該規則的合理例外。 你同意嗎? 如果是這樣,我如何才能將此特定組件聲明為該規則的例外?
定義@Component
angular允許將該組件僅用作html元素。 如果要使用它作為元素的屬性,請執行@Directive
import { Directive } from '@angular/core';
@Directive({
selector: '[app-footer]'
})
export class AppFooter {
.....
}
盡管這是一種不好的做法(根據樣式指南),但是仍然可以為此特定組件禁用此ts lint特定規則。
為此,請在組件注釋之前使用/ * tslint:disable:component-selector * /,以便組件聲明如下所示:
/* tslint:disable:component-selector */
@Component({
selector: '[app-footer]',
templateUrl: './app-footer.component.html'
})
export class AppFooterComponent
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.