简体   繁体   English

ESLint ES6 Redux global-required 意外的 require();

[英]ESLint ES6 Redux global-required Unexpected require();

在此处输入图像描述

I have this problem with ESLint and can't soulve on my own, those stores are separated for each enviroement as you can see on the screenshot, how could I fix that to make ESLint happy and I to learn a new thing?我在使用 ESLint 时遇到了这个问题,我自己无法解决,正如您在屏幕截图中看到的那样,这些商店在每个环境中都是分开的,我该如何解决这个问题才能使 ESLint 开心并让我学习新事物?

It's because you're requiring in branched code: http://eslint.org/docs/rules/global-require .这是因为您需要分支代码: http : //eslint.org/docs/rules/global-require

If you don't want to change your code, just add disabling comments:如果您不想更改代码,只需添加禁用注释:

/* eslint-disable global-require */

// your code here

/* eslint-enable global-require */

You can disable it in your .eslintrc file.您可以在.eslintrc文件中禁用它。

{
   rules: {
     "global-require": 0  
    }
}

You can also disable it inline:您也可以内联禁用它:

const facebookIcon = require('../../assets/images/facebook.svg'); // eslint-disable-line global-require

In my case, I imported image files in functional components like below.就我而言,我在功能组件中导入了图像文件,如下所示。

export const facebookIcon = require('../../assets/images/facebook.svg')
export const googleIcon = require('../../assets/images/google.svg')
export const logoboxImage = require('../../assets/images/logo-box.svg')

To avoid this lint error I just removed 'export' in front of images, it's gone.为了避免这个 lint 错误,我刚刚删除了图像前面的“导出”,它已经消失了。

const facebookIcon = require('../../assets/images/facebook.svg')
const googleIcon = require('../../assets/images/google.svg')
const logoboxImage = require('../../assets/images/logo-box.svg')

This is the way I did in 2022 without changing the rule这是我在 2022 年不改变规则的方式

<div :style="{backgroundImage: 'url(' + bg + ')'}" </div>
<script>
import bgImg from './assets/images/lg_bg.jpg';
export default {
    data() {
      return {
        bg: bgImg,
    };
  },    
}
</script>

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

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