【发布时间】: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