[英]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. 基本上,当打算只包含低级本机输入功能(如
button
或input
而不必在其周围放置<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.