Eslint throws被分配了一个值,但从未使用过,Webpack模块 [英] Eslint throws is assigned a value but never used , webpack module

查看:159
本文介绍了Eslint throws被分配了一个值,但从未使用过,Webpack模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将脚本导入Webpack,所有的工作正常,但是eslint抛出错误为模式分配了一个值,但从未使用过 。是否必须将const声明为全局变量或导出模块以修复错误?

I am importing a script in webpack, it all works but eslint is throwing the error 'modal is assigned a value but never used'. Do have to declare the const as a global or export the module to fix the error ?

modules.vanillaModal.js:

modules.vanillaModal.js :

import VanillaModal from 'vanilla-modal';

// Create instance
const modal = new VanillaModal({
  modal: '.c-modal',
  modalInner: '.js-modal__inner',
  modalContent: '.js-modal__content',
  open: '[rel="js-modal:open"]',
  close: '[rel="js-modal:close"]',
  class: 'js-modal--visible',
  loadClass: 'js-modal--loaded',
});

和我的webpack条目
index.js:

and my webpack entry index.js:

require('./modules.vanillaModal.js');


推荐答案

这是一个附带条件规则 http://eslint.org/docs/rules/no-unused-vars 。它会阻止您创建从未使用过的变量,这会导致代码混乱,或者可能意味着您正在使用的变量与您认为的不一样。

This is an eslint rule http://eslint.org/docs/rules/no-unused-vars. It prevents you from creating variables that you never use, which causes code clutter or could mean you're using variables that aren't what you think they are.

如果您使用的是设计不良的库,其中的类构造函数具有副作用(这是不应该的),并且您不需要对类的返回值做任何事情,我可以使用<禁用创建行的特定eslint规则a href = https://eslint.org/docs/2.13.1/user-guide/configuring#disabling-rules-with-inline-comments-1 rel = nofollow noreferrer>取消禁用评论 :

If you're using a poorly designed library where the class constructor has side effects (which it isn't supposed to), and you don't need to do anything with the returned value from the class, I would disable that specific eslint rule for the create line with eslint disable comments:

// eslint-disable-next-line no-unused-vars
const modal = new VanillaModal({
  modal: '.c-modal',
  modalInner: '.js-modal__inner',
  modalContent: '.js-modal__content',
  open: '[rel="js-modal:open"]',
  close: '[rel="js-modal:close"]',
  class: 'js-modal--visible',
  loadClass: 'js-modal--loaded',
});

您还可以使用eslint特定注释包装任何代码块,以禁用该块的规则:

You can also wrap any block of code with eslint specific comments to disable a rule for that block:

/* eslint-disable no-unused-vars */
const modal = new VanillaModal({
    ...
});
/* eslint-enable no-unused-vars */

这篇关于Eslint throws被分配了一个值,但从未使用过,Webpack模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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