简体   繁体   English

Angular - 使用组件选择器作为属性使tslint生气

[英]Angular - Using a component selector as an attribute makes tslint get angry

I'm trying to create a component with an attribute as a selector, like this: 我正在尝试创建一个具有属性作为选择器的组件,如下所示:

@Component({
    selector: '[my-attribute-selector]',
    template: ``
})
export class MyComponent { 
   // Some cool stuff
}

However, tslint is complaining about that, with the following message: 然而,tslint抱怨这一点,并带有以下消息:

[tslint] The selector of the component "MyComponent" should be used as element

I know I could just disable that tslint rule, but I'd like to know if there's a reasonable reason why I shouldn't use an attribute as the component's selector before doing so. 我知道我可以禁用该tslint规则,但我想知道在这样做之前是否有合理的原因我不应该使用属性作为组件的选择器。

Thanks in advance! 提前致谢!

Your tslint.config file will be having this rule 您的tslint.config文件将具有此规则

"component-selector": [
  "element",
  "app",
  "kebab-case"
],

Please modify that to allow attribute selector as below 请修改它以允许attribute选择器如下所示

"component-selector": [
  "attribute", 
  "myPrefix", 
  "camelCase"
]

To allow linting for both element and attribute selectors, in tslint.config pass in an array ["element", "attribute"] instead of "element" or just "attribute" : 以允许掉毛两种元素和属性选择器,在tslint.config通在阵列["element", "attribute"]代替"element"或只是"attribute"

"component-selector": [
        true,
        ["element", "attribute"],
        "app",
        "kebab-case"
    ]

As per reasons of taking the attribute approach, I will quote from this issue on codelyzer . 根据采用属性方法的原因,我将在codelyzer上引用此问题 Basically it's advisable when intending to just wrap low-level native input functionality like button or input without having to put a <my-custom-input> around them. 基本上,当打算只包含低级本机输入功能(如buttoninput而不必在其周围放置<my-custom-input>时,这是明智的。

After listening to Angular Material team's talk in ng-conf about component API designs there is a case for using attribute selectors as components to allow components to be able to use the DOM API without needing to reflect 20+ bindings from the custom element style components to the inner DOM API it wraps. 在听取Ang-Material团队关于组件API设计的讨论之后,有一种情况是使用属性选择器作为组件,以允许组件能够使用DOM API,而无需反映自定义元素样式组件的20多个绑定。它包装的内部DOM API。

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

相关问题 在Angular CLI中使用tslint - Using tslint with Angular CLI 角度2组件可以与属性选择器一起使用吗? - Can an angular 2 component be used with an attribute selector? 绑定属性值上的 Angular 组件选择器 - Angular component selector on a bound attribute value Angular2 - 是否可以使用选择器名称获取组件类名称 - Angular2 - is it possible to get component class name using selector name 检查组件是否在angular2中具有属性选择器 - Check if component have attribute selector in angular2 angular 指令选择器和 tslint 之间的冲突 - The conflict between angular directive selector and tslint 在Angular 4中将组件选择器用于其他组件 - Using a component selector into other component in Angular 4 我可以使用角度组件选择器作为该组件的属性吗? - Can I use an angular component selector as an attribute for that component? 使用 Protractor,如果该“元素”是具有 id 属性的 angular 组件选择器,您能否通过 id 找到元素? - Using Protractor, can you find an element by id if that “element” is an angular component selector with an id attribute? 如何通过 Angular CLI 生成带有属性选择器的 Angular 组件 - How to generate an Angular component with an attribute selector via Angular CLI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM