Linter错误-未定义仅浏览器功能 [英] Linter error - Browser-only function is not defined

查看:87
本文介绍了Linter错误-未定义仅浏览器功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我依靠的是浏览器中可用的类FileReader

I'm relying on a class that is available in browser, FileReader

我在webpack中不断收到错误-'FileReader' is not defined no-undef

I keep getting an error in webpack - 'FileReader' is not defined no-undef

处理此问题的正确方法是什么?我目前正在使用一种忽略消息的方法.

What is the correct way of dealing with this? I'm currently using a method where I just ignore the message.

推荐答案

问题在于,由于webpack不能作为Node.js的一部分找到它,并且由于它不可用,因此会导致错误.但是有几种方法可以解决这个问题.

The issue is that since webpack cannot find it as part of Node.js and since it is not available, it will cause an error. But there are a few ways of getting around this.

代替

var reader = new FileReader();

使用

var reader = new window.FileReader();

修复#2

var reader = new global.FileReader();

默认情况下,

webpack将全局转换为window. 有关更多信息,请访问: https://webpack.js.org/configuration/node/

webpack, by default, will convert global to window. More info at: https://webpack.js.org/configuration/node/

// in webpack.config.js
module.exports = {
  //...
  externals: {
    FileReader: 'FileReader'
  }
};

更多信息: https://webpack.js.org/configuration/externals/

这篇关于Linter错误-未定义仅浏览器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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