简体   繁体   English

避免出现错误“组件的选择器应命名为kebab-case并包含破折号”

[英]Avoid error 'The selector of the component should be named kebab-case and include dash '

How could I avoid error 'The selector of the component should be named kebab-case and include dash ' in Angular2, if I don't want to rename the selector? 如果我不想重命名选择器,如何避免在Angular2中出现错误“组件的选择器应命名为kebab-case并包含破折号”? Is there any workaround?.. 有什么解决方法吗?

Try 尝试

import {CUSTOM_ELEMENTS_SCHEMA} from "@angular/core";

@NgModule({
    schemas: [
        CUSTOM_ELEMENTS_SCHEMA
    ]
})

This error is being generated by TSLint, using the codelyzer plugin. TSLint正在使用codelyzer插件生成此错误。 This enforces the Angular styleguide recommendations for selectors: https://angular.io/guide/styleguide#component-selectors 这将对选择器实施Angular样式指南建议: https ://angular.io/guide/styleguide#component-selectors

As with most linting errors, there are ways to configure or ignore them. 与大多数掉毛错误一样,有多种方法可以配置或忽略它们。 Be sure to be deliberate about how and when you ignore linting errors. 请务必谨慎考虑如何以及何时忽略掉毛错误。 Depending on your situation, you may want to handle this error differently: 根据您的情况,您可能希望以不同的方式处理此错误:

  • If all your components have this error, and have matching selectors, you can change the rules TSLint uses to check for errors in the TSLint.json file: 如果所有组件都出现此错误,并且具有匹配的选择器,则可以更改TSLint用于检查TSLint.json文件中的错误的规则:

     { "rules": { // The rules component-selector and directive-selector have the following arguments: // [ENABLED, "attribute" | "element", "prefix" | ["listOfPrefixes"], "camelCase" | "kebab-case"] "component-selector": [true, "element", ["cmp-prefix1", "cmp-prefix2"], "kebab-case"], "directive-selector": [true, "attribute", ["dir-prefix1", "dir-prefix2"], "camelCase"], ... } } 

    see https://github.com/mgechev/codelyzer#recommended-configuration 参见https://github.com/mgechev/codelyzer#recommended-configuration

    You can also configure the CLI to generate components and directives with the correct prefix at the bottom of your angular.json file: 您还可以配置CLI以在angular.json文件底部生成具有正确前缀的组件和指令:

     { "schematics": { "@schematics/angular:component": { "inlineStyle": false, "inlineTemplate": false, "prefix": "myCustomPrefix", "styleext": "scss" }, "@schematics/angular:directive": { "prefix": "myCustomPrefix" } } 
  • If you have a one-off component or directive you would like to exclude TSLint from checking, you can use the suppression comment: 如果您有一次性组件或指令要从检查中排除TSLint,则可以使用抑制注释:

     // tslint:disable-next-line:component-selector 
  • If you would like to disable this checking completely, you can do so in tslint.json : 如果您想完全禁用此检查,可以在tslint.json这样做:

     { "rules": { "directive-selector": false, "component-selector": false, ... } } 

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

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