reactjs tslint 说不允许调用 console.log - 我该如何允许?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49990513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
tslint says calls to console.log are not allowed - How do I allow this?
提问by PatS
I just started using create-react-app with typescript
我刚开始使用带有打字稿的 create-react-app
create-react-app my-app --scripts-version=react-scripts-ts
and the default tslint.json configuration does not allow console.log().
并且默认的 tslint.json 配置不允许 console.log()。
How can I (for now) enable console.log?
我如何(现在)启用 console.log?
The docs for this are at https://palantir.github.io/tslint/rules/no-console/. But they don't say where to put this line:
此文档位于 https://palantir.github.io/tslint/rules/no-console/。但他们没有说把这条线放在哪里:
"no-console": [true, "log", "error"]
I searched and found this tslint.json configuration file syntax, so I tried this:
我搜索并找到了这个tslint.json 配置文件语法,所以我尝试了这个:
"rules": {
"no-console": [true, "warning"]
}
In an attempt to get log messages that would just be warnings. But that didn't work.
试图获取只是警告的日志消息。但这没有用。
I've commented out the few console.log() lines I have but will want to be able to do this in the future.
我已经注释掉了我拥有的几行 console.log() ,但希望将来能够做到这一点。
回答by Christian Ivicevic
Add // tslint:disable-next-line:no-consolein the line right before your calls to console.logto prevent the error message only once.
// tslint:disable-next-line:no-console在您调用之前的行中添加console.log以防止错误消息只出现一次。
If you want to disable the rule entirely add the following to your tslint.json(most likely in your root folder):
如果您想完全禁用该规则,请将以下内容添加到您的tslint.json(很可能在您的根文件夹中):
{
"rules": {
"no-console": false
}
}
回答by Lee Brindley
For those of you coming here with a mixed codebase of javascript and typescript.
对于那些带着 javascript 和 typescript 混合代码库来到这里的人。
You may need to define the 'no-console' option in jsRules, jslints rules object for javascript files, i.e. there are separate rules objects for javascript and typescript.
您可能需要在 jsRules 中定义“no-console”选项,javascript 文件的 jslints 规则对象,即 javascript 和 typescript 有单独的规则对象。
//tslint.json
//tslint.json
{
"extends": ["tslint:recommended", "tslint-react", "tslint-config-prettier"], //Example...
"rules": {
"no-console": false //Disable for typescript
},
"jsRules": {
"no-console": false //Disable for javascript
}
}
回答by Liu Xuan
Add the following to your tslint.json
将以下内容添加到您的 tslint.json
{
"rules": {
"no-console": {
"severity": "warning",
}
}
}
回答by Liran H
This is the correct syntax to define the no-console rule (or any other rule for that matter) but only with a warning rather than an error (obviously change the options to whatever you want)
这是定义无控制台规则(或任何其他与此相关的规则)的正确语法,但只有警告而不是错误(显然将选项更改为您想要的任何内容)
"no-console": {
"severity": "warning",
"options": [
"log",
"error",
"debug",
"info",
"time",
"timeEnd",
"trace"
]
},
回答by alveomaster
in typeScript version 3 update tslint.json under key rule like below:
在 typeScript 版本 3 中,在如下关键规则下更新 tslint.json:
"no-console": [
true,
"debug",
"time",
"timeEnd",
"trace"
],
this way you just specify debug, time, timeEnd, trace to be not used, if in your default tslint "info" is in the list just remove it.
这样您只需指定不使用的调试、时间、时间结束、跟踪,如果在您的默认 tslint 中“信息”在列表中,只需将其删除。
回答by Adel Balbisi
if // tslint:disable-next-line:no-consoledoesn't work try with // eslint:disable-next-line:no-console
如果// tslint:disable-next-line:no-console不起作用尝试// eslint:disable-next-line:no-console
回答by Rashid Iqbal
回答by Domiserver
The way I handle tslint "no-console" ruleis per file which I have found is convenient and isolated in the development phase.
我处理tslint“无控制台”规则的方式是每个文件,我发现在开发阶段很方便且隔离。
As soon as I need to use the first console.log(); Visual Studio Code shows the option to add:
只要我需要使用第一个 console.log(); Visual Studio Code 显示了添加选项:
// tslint:disable-next-line: no-console
console.log();
// tslint:disable-next-line: 无控制台
控制台日志();
So here I just delete "-next-line" and this command will cover the entire file.
所以在这里我只是删除“-next-line”,这个命令将覆盖整个文件。
// tslint:disable: no-console
console.log();
// tslint:disable: 无控制台
控制台日志();
I hope it helps as an alternative to disable the feature for the entire app.
我希望它有助于作为禁用整个应用程序的功能的替代方法。
RON
罗恩
回答by loretoparisi
According to the docs: https://eslint.org/docs/user-guide/getting-started#configuration
根据文档:https: //eslint.org/docs/user-guide/getting-started#configuration
- "off" or 0 - turn the rule off
- "warn" or 1 - turn the rule on as a warning (doesn't affect exit code)
- "error" or 2 - turn the rule on as an error (exit code will be 1)
- "off" 或 0 - 关闭规则
- "warn" 或 1 - 打开规则作为警告(不影响退出代码)
- "error" 或 2 - 将规则作为错误打开(退出代码将为 1)
By the way, your correct setup would be
顺便说一句,您的正确设置是
{
"rules": {
"no-console": false
}
}


