简体   繁体   English

为什么Angular在组件名称中不需要破折号

[英]Why does Angular not need a dash in component name

I wondered why Polymer elements need a dash in custom elements name like <my-component> while this is not necessary for Angular components especially as Angular components also use ShadowDOM. 我想知道为什么Polymer元素需要在自定义元素名称中使用破折号,例如<my-component>而Angular组件不需要这样,特别是Angular组件也使用ShadowDOM。

** Edit** It doesn't even seem to be suggested good practice in Angular. **编辑**它似乎甚至没有被建议在Angular中做好。

The HTML spec allows you to use unknown tags ( <tab> , <panel> ) without the HTML parser throwing a fit. HTML规范允许您使用未知标记( <tab><panel> ),而不使用HTML解析器。 To their benefit, Angular uses this behavior for their directives to make the components like native. 为了他们的利益,Angular将这种行为用于指令,使组件像本机一样。

Tags that don't have a - inherit from HTMLUnknownElement . 没有-HTMLUnknownElement继承的HTMLUnknownElement There's a good explanation of the upgrade process here: HTML5Rocks - Custom Elements - How elements are upgraded 这里有一个很好的解释升级过程: HTML5Rocks - 自定义元素 - 如何升级元素

Since Angular directives were designed in a time before the Custom Elements spec existed, they don't use a - . 由于Angular指令是在自定义元素规范存在之前的一段时间内设计的,因此它们不使用- It's the standard that requires element names contain a - . 这是要求元素名称包含-的标准。

The dash is not required by Angular since there is no technical reason to require it. Angular不需要破折号,因为没有技术理由要求它。 However, all large projects I have worked on prefix all components and directives with a two character and then dash prefix, eg "ab-tab". 但是,我所做的所有大型项目都在前缀所有组件和指令前加上两个字符,然后是短划线前缀,例如“ab-tab”。

First, using dashes in names makes your syntax compatible with the Custom Elements standard, although Angular doesn't depending the spec. 首先,在名称中使用短划线使您的语法与自定义元素标准兼容,尽管Angular不依赖于规范。

Second, it helps with organization. 其次,它有助于组织。 Standard Angular directives are all prefixed with 'ng-'. 标准Angular指令都以'ng-'为前缀。 By using your own prefix, people reading your code will be able to quickly distinguish between components from different libraries. 通过使用您自己的前缀,阅读代码的人将能够快速区分不同库中的组件。

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

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