解决文档的linter错误no-undef [英] Solving linter error no-undef for document

查看:79
本文介绍了解决文档的linter错误no-undef的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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)在文件中:


  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:


    1. 在配置中将环境设置为浏览器

    {
      "env": {
        "browser": true,
        "node": true
      }
    }
    


  • 在配置中将其添加为全局变量:

  • Add it as a global in the configuration:

    {
      "globals": {
        "document": false
      }
    }
    


  • 3)从CLI:


    1. 使用env: eslint --env浏览器,节点file.js

    使用全局变量: eslint --global document file.js

    资源:

    使用ESLint指定全局变量

    指定环境具有ESLint的批处理

    通过ESLint CLI进行环境

    使用ESLint CLI指定全局变量

    这篇关于解决文档的linter错误no-undef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆