简体   繁体   English

忽略 moment.js 的 eslint 错误

[英]Ignoring eslint error for moment.js

I am adding moment.js in my react app as an external resource like this:我在我的 React 应用程序中添加 moment.js 作为外部资源,如下所示:

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

When I use moment in my code file where eslint is enabled then I am getting this eslint error: 'moment' is not defined(no-undef) .当我在启用eslint代码文件中使用moment ,我收到此 eslint 错误: 'moment' is not defined(no-undef)

This is my .jsx file-这是我的 .jsx 文件-

constructor() {
  super()
  this.state = {
    startDate: moment()
  }
}
...

My eslintrc.yml look like this:我的eslintrc.yml看起来像这样:

extends:
- jquery
- airbnb

parser: babel-eslint

parserOptions:
  ecmaVersion: 6
  ecmaFeatures:
    experimentalObjectRestSpread: true
    jsx: true
  sourceType: "module"

env:
  browser: true
  node: true
  jquery: true
...

If you use webpack.如果你使用 webpack。 npm install moment --save import moment from moment; npm install moment -- 从时刻保存导入时刻;

Or you can disable linting by adding a comment after moment() in the same line.或者您可以通过在同一行的moment()之后添加注释来禁用 linting。 // eslint-disable-line

If using is a hard requirement如果使用是一个硬性要求

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

But you can edit the .eslintrc file, you can tell eslint that you have made moment available in the global namespace, which is what your script tag is doing.但是你可以编辑.eslintrc文件,你可以告诉eslint你已经在全局命名空间中提供了 moment,这就是你的脚本标签正在做的事情。

{
//...
    "env": {
        "moment": true
    }
//...
}

Link to eslint environment docs 链接到eslint环境文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM