使用 Echarts 时,控制台警告:
Added non-passive event listener to ascroll- blocking 'mousewheel’event Consider marking event handler as ’passive’ to make the page more responsive
// 1. 安装依赖
npm i import default-passive-events
// 2. 项目 main.ts 引入
import 'default-passive-events';
使用 vben 的分页器,在修改pageSize时会报错,与依赖 default-passive-events 有关,报错如下:
Unable to preventDefault inside passive event listener invocation
通过修改 EventTarget.prototype.addEventListener
方法来解决这个问题。在新定义的方法中,代码检查了 capture
参数是否为布尔值。如果 capture
参数不是布尔值,代码将其设置为一个空对象,并将 passive
属性设置为 false
。
通过对 addEventListener
方法进行修改,确保在没有明确设置 capture
参数为 true
的情况下,其默认值为 false
。这样一来,就避免了在触发事件时出现报错的问题。
(function () {
if (typeof EventTarget !== 'undefined') {
const func = EventTarget.prototype.addEventListener;
EventTarget.prototype.addEventListener = function (type, fn, capture) {
(this as any).func = func;
if (typeof capture !== 'boolean') {
capture = capture || {};
capture.passive = false;
}
(this as any).func(type, fn, capture);
};
}
})();
// 在main.ts中引入
import '@/utils/browserPatch';