简体   繁体   English

Lint angular html 模板中的属性顺序

[英]Lint the attribute order in angular html templates

At the moment we discuss during code reviews the order of attributes.目前我们在代码审查期间讨论属性的顺序。 We want to avoid the effort und think it is better to be supported by IDE or tool.我们想避免这种努力,我们认为最好得到 IDE 或工具的支持。

Someone knows a good tool (and if not a best practices sheet)?有人知道一个好的工具(如果不是最佳实践表)?

I've just found this VSCode extension to apply an order to the attributes of HTML, and it seems to work fine (although it messed up when I've selected the whole document... works good, though, when selecting only the element + attributes that I've wanted to sort):我刚刚发现这个 VSCode 扩展将订单应用于 HTML 的属性,它似乎工作正常(虽然当我选择了整个文档时它搞砸了......不过,当只选择元素时效果很好+我想要排序的属性):

Split HTML Attributes (Vue, React, Angular) --> https://marketplace.visualstudio.com/items?itemName=dannyconnell.split-html-attributes拆分 HTML 属性(Vue、React、Angular)-> https://marketplace.visualstudio.com/items?itemName=dannyconnell.split-html-attributes

From angular version 14 in 2022-2023 we can use @angular-eslint/template/attributes-order rule to order angular template attributes.从 angular 2022-2023 版本 14 我们可以使用@angular-eslint/template/attributes-order规则对 angular 模板属性进行排序。

example for eslintrc.js : eslintrc.js的示例:

...
{
 files: ['*.html'],
 extends: ['plugin:@angular-eslint/template/recommended'],
 rules: {
  '@angular-eslint/template/attributes-order': [
     'error',
     {
     alphabetical: false,
     order: [
       'STRUCTURAL_DIRECTIVE', // e.g. `*ngIf="true"`, `*ngFor="let item of items"`
       'TEMPLATE_REFERENCE', // e.g. `<input #inputRef>`
       'CLASS',
       'ATTRIBUTE_BINDING', // e.g. `<input required>`, `id="3"`
       'INPUT_BINDING', // e.g. `[id]="3"`, `[attr.colspan]="colspan"`, [style.width.%]="100", [@triggerName]="expression", `bind-id="handleChange()"`
       'TWO_WAY_BINDING', // e.g. `[(id)]="id"`, `bindon-id="id"
       'OUTPUT_BINDING', // e.g. `(idChange)="handleChange()"`, `on-id="handleChange()"`
     ],
   },
 ],
}
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM