【问题标题】:Tslint: JSX elements with no children must be self closing [Error]Tslint:没有子元素的 JSX 元素必须自关闭 [错误]
【发布时间】:2020-05-25 10:45:56
【问题描述】:

所以我一直在寻找解决这个问题的方法。我的解决方案不会通过命令npm run build 构建,因为我有错误:

没有子元素的 JSX 元素必须是自闭合的。

这里有一个类似的问题,没有接受(或有效)的答案:JSX elements with no children must be self-closing

关联的Typescript/HTML的格式为:

class ExampleClass { 
    render() {
           return <div>
               <div> 
                   <div>Content 1</div>
                   <div>Content 2</div>
                   <div>Content 3</div>
              </div>
          </div>;
      }
}
export default ExampleClass;

“错误”发生在第 5 行,即:

<div>Content 1</div>

我正在使用 Tslint 并且 Tslint 的许多功能已经更改/在文件 tslint.json 中工作。

查看当前的tslint.json 文件:

{
  "extends": ["tslint:recommended", "tslint-react", "tslint-config-prettier"],
  "linterOptions": {
    "exclude": [
      "gulpfile.js",
      "bin/**",
      "wwwroot/**",
      "config/**/*.js",
      "node_modules/**"
    ]
  },
  "rules": {
    "prefer-const": false,
    "triple-equals": false,
    "jsx-no-lambda": false,
    "object-literal-shorthand": false,
    "no-shadowed-variable": false,
    "ban-types": false,
    "one-variable-per-declaration": false,
    "callable-types": false,
    "quotemark": [ false, "single", "jsx-double" ],
    "indent": [ true, "spaces", 2 ],
    "interface-name": false,
    "ordered-imports": false,
    "object-literal-sort-keys": false,
    "no-consecutive-blank-lines": false,
    "comment-format": false,
    "no-trailing-whitespace": false,
    "one-line": false,
    "max-classes-per-file": false,
    "jsx-boolean-value": false,
    "no-empty-interface": false,
    "variable-name": [ true, "allow-pascal-case", "allow-snake-case" ],
    "no-console": false,
    "interface-over-type-literal": false
  }
}

以下是我尝试过的各种方法(连续尝试,不是一次全部尝试)- 均未成功:

"prefer-const": [
      true,
      { "destructuring": "all" }
    ],
"react/self-closing-comp": "off",
"react/self-closing-comp": false,
"no-trailing-whitespace":  false

Tslint 的规则可以在这里找到:TSLint core rules

想做的是:

  • 完全禁用 TSLint
  • 除非完全必要,否则修改我的 HTML 结构

我正在寻找的是用于抑制此错误的正确 Tslint 规则。
"react/self-closing-comp": false.

希望有人以前见过这个并且可以帮我一把!

非常感谢!

【问题讨论】:

    标签: html reactjs typescript jsx tslint


    【解决方案1】:

    根据截至 2020 年 1 月 20 日的npmjs.com

    TSLint 已被弃用,取而代之的是 ESLint。请参阅https://github.com/palantir/tslint-react/issues/210 了解更多信息。

    您可以配置您现有的 TSLint 解决方案以使用来自 ESLint 的新规则,这样做是这样的:

    1. 根据npmjs.com,使用npm命令安装ESLint:npm install eslint --save-dev
    2. 根据npmjs.com,通过运行以下命令来允许ESLint规则:npm install --save-dev tslint-eslint-rules
    3. 通过添加extends 属性来修改您的tslint.json 文件,如下所示:"extends": [ "tslint-eslint-rules"]

    这里有很多相关的 ESLint 规则:ESLint Rules - npmjs.com 和这里 ESLint Rules - eslint.org

    修复错误的相关规则:

    没有子元素的 JSX 元素必须是自闭合的。

    是这样吗:

     "jsx-self-close": false
    

    我的最终tslint.json 文件如下所示:

    {
      "extends": [ "tslint-eslint-rules", "tslint:latest", "tslint-react", "tslint-config-prettier" ],
      "linterOptions": {
        "exclude": [
          "gulpfile.js",
          "bin/**",
          "wwwroot/**",
          "config/**/*.js",
          "node_modules/**"
        ]
      },
      "rules": {
        "jsx-self-close": false,
        "jsx-wrap-multiline": false,
        "no-constant-condition": true,
        "no-unused-expression": false,
        "no-unused-variable": false,
        "no-string-throw": false,
        "prefer-const": false,
        "triple-equals": false,
        "jsx-no-lambda": false,
        "object-literal-shorthand": false,
        "no-shadowed-variable": false,
        "ban-types": false,
        "one-variable-per-declaration": false,
        "callable-types": false,
        "quotemark": [ false, "single", "jsx-double" ],
        "indent": [ true, "spaces", 2 ],
        "interface-name": false,
        "ordered-imports": false,
        "object-literal-sort-keys": false,
        "no-consecutive-blank-lines": false,
        "comment-format": false,
        "no-trailing-whitespace": false,
        "one-line": false,
        "max-classes-per-file": false,
        "jsx-boolean-value": false,
        "no-empty-interface": false,
        "variable-name": false,
        "no-console": false,
        "interface-over-type-literal": false,
        "no-conditional-assignment": false,
        "only-arrow-functions": false,
        "no-var-keyword": false,
        "no-empty": false,
        "no-submodule-imports": false,
        "no-duplicate-imports": false,
        "no-useless-rename": false,
        "no-implicit-dependencies": false,
        "no-return-await": false,
        "no-object-literal-type-assertion": false,
        "prefer-object-spread": false,
        "prefer-conditional-expression": false,
        "jsdoc-format": false,
        "prefer-for-of": false,
        "radix": false
      }
    }
    

    希望这可以节省一些时间!

    【讨论】:

      猜你喜欢
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 2023-02-13
      • 2017-08-30
      • 2022-01-05
      • 2020-03-09
      • 2021-12-13
      相关资源
      最近更新 更多