解决文档的linter错误no-undef [英] Solving linter error no-undef for document
问题描述
我正在使用 airbnb
扩展名来整理我的React项目。现在,在我的 index.js
中,我有:
I am using airbnb
extension for linting my React Project. Now, in my index.js
I have:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<App />,
document.getElementById('root'),
);
lint说:
no-undef 'document' is not defined.at line 8 col 3
我该如何解决这个问题?
How can I solve this problem?
推荐答案
有很多解决方法。两种主要方法是将 document
指定为 global
或设置 eslint-env
作为浏览器
(您可能想要的)。您可以执行以下操作:1)在文件中,2)在配置中,甚至3)从CLI运行时。
There are a number of ways to solve/get around this. The two key ways are to either specify document
as global
or to set the eslint-env
as browser
(what you probably want). You can do this 1) in-file, 2) in the configuration, or even 3) when running from the CLI.
1)在文件中:
-
在文件中将环境设置为
浏览器
:
/* eslint-env browser */
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<App />,
document.getElementById('root'),
);
将其添加为文件本身的全局变量:
Add it as a global in the file itself:
/* global document */
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<App />,
document.getElementById('root'),
);
2)在eslint配置中:
2) In the eslint configuration:
-
在配置中将环境设置为
浏览器
:
{
"env": {
"browser": true,
"node": true
}
}
在配置中将其添加为全局变量:
Add it as a global in the configuration:
{
"globals": {
"document": false
}
}
3)从CLI:
-
使用env:
eslint --env浏览器,节点file.js
使用全局变量: eslint --global document file.js
资源:
使用ESLint指定全局变量
指定环境具有ESLint的批处理
通过ESLint CLI进行环境
使用ESLint CLI指定全局变量
这篇关于解决文档的linter错误no-undef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!