angular2如何更改组件的默认前缀以停止tslint警告

Neh*_*nia 122 angular

看来,当我使用Angular cli创建一个Angular 2应用程序时.我的默认组件前缀是AppComponent的app-root.现在,当我将选择器更改为其他名称为"abc-root"时

@Component({
  selector: 'abc-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
Run Code Online (Sandbox Code Playgroud)

vscode警告我,

[tslint] The selector of the component "AppComponent" should have prefix "app"
Run Code Online (Sandbox Code Playgroud)

Neh*_*nia 254

您需要修改两个文件tslint.json和.angular-cli.json,假设您要更改为myprefix:

在tslint.json文件中,只需修改以下2个属性:

"directive-selector": [true, "attribute", "app", "camelCase"],
"component-selector": [true, "element", "app", "kebab-case"],
Run Code Online (Sandbox Code Playgroud)

将"app"更改为"myprefix"

"directive-selector": [true, "attribute", "myprefix", "camelCase"],
"component-selector": [true, "element", "myprefix", "kebab-case"],
Run Code Online (Sandbox Code Playgroud)

在angular.json文件中只需修改属性前缀:( 对于小于6的角度版本,文件名为.angular-cli.json)

"app": [
  ...
  "prefix": "app",
  ...
Run Code Online (Sandbox Code Playgroud)

将"app"更改为"myprefix"

"app": [
  ...
  "prefix": "myprefix",
  ...
Run Code Online (Sandbox Code Playgroud)

如果在这种情况下你需要多个前缀@Salil Junior指出:

"component-selector": [true, "element", ["myprefix1", "myprefix2"], "kebab-case"],
Run Code Online (Sandbox Code Playgroud)

如果使用Angular cli创建新项目,请使用此命令行选项

ng new project-name --prefix myprefix
Run Code Online (Sandbox Code Playgroud)

  • 请注意,在Angular 6中,可以在`<your-project>/src/tslint.json`中找到另一个`tslint.json`文件,其中包含组件和指令选择器规则.如果您已应用上述修复但仍然无法正常工作,请确保检查此文件是否未覆盖您的全局配置.(https://github.com/mgechev/codelyzer/issues/620#issuecomment-394131604) (10认同)
  • 即使在更新`tslint.json`之后,我也从'ng generate component`收到警告:`你正在使用app的不同前缀,你可能会得到lint错误.请相应地更新"tslint.json".我必须更新`.angular-cli.json`中的`apps.prefix`属性以消除该警告. (2认同)

小智 18

  1. 调整你的angular-cli.json:"prefix":"defaultPrefix",以便angular-cli将使用它来生成组件.
  2. tslint.json像这样的Ajust :

    "directive-selector": [
      true,
      "attribute",
      ["prefix1", "prefix2", "prefix3"],
      "camelCase"
    ],
    "component-selector": [
      true,
      "element",
      ["prefix1", "prefix2", "prefix3"],
      "kebab-case"
    ],
    
    Run Code Online (Sandbox Code Playgroud)


And*_*lva 16

实际上,使用Angular Cli,您只需更改angular-cli.json位于根应用程序上的"apps"数组内的"prefix"标记.

改变"TheBestPrefix",就像这样.

"apps": [
  {
    "root": "src",
    "outDir": "dist",
    "assets": [
      "assets",
      "favicon.ico"
    ],
    "index": "index.html",
    "main": "main.ts",
    "test": "test.ts",
    "tsconfig": "tsconfig.json",
    "prefix": "TheBestPrefix",
    "mobile": false,
    "styles": [
      "styles.css"
    ],
    "scripts": [],
    "environments": {
      "source": "environments/environment.ts",
      "dev": "environments/environment.ts",
      "prod": "environments/environment.prod.ts"
    }
  }
]
Run Code Online (Sandbox Code Playgroud)

使用CLI生成新组件时,ng g component mycomponent 组件标记将具有以下名称"TheBestPrefix-mycomponent"


Ani*_*Das 11

对于angular 6/7以后会有一个tslint.json你里面/src的文件夹,其保存tslist为组件和指令的规则。

{
    "extends": "../tslint.json",
    "rules": {
        "directive-selector": [
            true,
            "attribute",
            "directivePrefix",
            "camelCase"
        ],
        "component-selector": [
            true,
            "element",
            "compoenent-prefix",
            "kebab-case"
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

更改该文件将解决此问题。

  • 或将其删除,以使其不再覆盖主要的`tslint.json`文件。 (2认同)