Javascript 解决文档的 linter 错误 no-undef
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41858052/
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
Solving linter error no-undef for document
提问by FacundoGFlores
I am using airbnbextension for linting my React Project. Now, in my index.jsI have:
我正在使用airbnb扩展来检查我的 React 项目。现在,在我的index.js我有:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(
<App />,
document.getElementById('root'),
);
linter says:
linter 说:
no-undef 'document' is not defined.at line 8 col 3
How can I solve this problem?
我怎么解决这个问题?
回答by Nick Bartlett
There are a number of ways to solve/get around this. The two key ways are to either specify documentas globalor to set the eslint-envas browser(what you probably want). You can do this 1) in-file, 2) in the configuration, or even 3) when running from the CLI.
有很多方法可以解决/解决这个问题。两种关键方法是指定document为global或设置eslint-env为browser(您可能想要的)。您可以在 1) 文件中执行此操作,2) 在配置中执行此操作,甚至 3) 在从 CLI 运行时执行此操作。
1) In-file:
1) 文件内:
Set the environment as
browserin your file:/* 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'), );
browser在您的文件中设置环境:/* eslint-env browser */ import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; ReactDOM.render( <App />, document.getElementById('root'), );将其添加为文件本身的全局变量:
/* global document */ import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; ReactDOM.render( <App />, document.getElementById('root'), );
2) In the eslint configuration:
2)在eslint配置中:
Set the environment as
browserin the configuration:{ "env": { "browser": true, "node": true } }Add it as a global in the configuration:
{ "globals": { "document": false } }
browser在配置中设置环境:{ "env": { "browser": true, "node": true } }在配置中将其添加为全局:
{ "globals": { "document": false } }
3) From the CLI:
3)从CLI:
Using env:
eslint --env browser,node file.jsUsing globals:
eslint --global document file.js
使用环境:
eslint --env browser,node file.js使用全局变量:
eslint --global document file.js
Resources:
Specifying Globals with ESLint
Specifying Environments with ESLint
Specifying env with ESLint CLI
Specifying globals with ESLint CLI

