Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

编译小程序后自带了window对象,导致ECharts插件注册事件失败 #10374

Closed
johnlucita opened this issue Sep 30, 2021 · 2 comments
Labels
F-vue3 Framework - Vue 3 T-weapp Target - 编译到微信小程序 V-3 Version - 3.x

Comments

@johnlucita
Copy link

相关平台

微信小程序

小程序基础库: 2.19.5
使用框架: Vue 3

复现步骤

小程序 引用echarts-taro3-vue组件

期望结果

小程序是封装了window对象的,因此大多通用的组件都会采用类似typeof window !== 'undefined' && !!window.addEventListener;这样的判断条件来区分事件绑定方式是使用addEventListener或者attachEvent,可否在编译小程序时去除window对象,以保证小程序端代码执行准确

实际结果

Taro编译的小程序中判断条件typeof window !== 'undefined' && !!window.addEventListener;返回true,导致组件注册事件走了平台代码,错误信息参考Cecilxx/echarts-taro3-vue#5

环境信息

Taro CLI 3.3.7 environment info:
    System:
      OS: Windows 10
    Binaries:
      Node: 14.16.1 - C:\Program Files\nodejs\node.EXE
      Yarn: 1.22.10 - ~\AppData\Roaming\npm\yarn.CMD
      npm: 6.14.12 - C:\Program Files\nodejs\npm.CMD

补充信息

使用echarts-taro3-vue组件在小程序端绘制图表

@taro-bot2 taro-bot2 bot added F-vue3 Framework - Vue 3 T-weapp Target - 编译到微信小程序 V-3 Version - 3.x labels Sep 30, 2021
@wujianping334455
Copy link

遇到同样的问题,echarts 一直报el.addEventListener is not a function,3.2.0以下版本都没有问题,升到3.3.12就有问题了。没有仔细查阅,不知道到底是哪个版本改了window

@Chen-jj
Copy link
Contributor

Chen-jj commented Dec 15, 2021

Taro框架自带了window对象,这与ECharts的判断条件(var isDomLevel2 = typeof window !== 'undefined' && !!window.addEventListener;)起了冲突,导致判断错误,走了平台分支导致的

正解。

因此手动修改 isDomLevel2 的值即可:

// echarts-taro3-vue/src/components/ec-canvas/echarts.js Line:1801

// 修改前:
var isDomLevel2 = typeof window !== 'undefined' && !!window.addEventListener;

// 修改后:
var isDomLevel2 = process.env.TARO_ENV === 'h5'

@Chen-jj Chen-jj closed this as completed Dec 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-vue3 Framework - Vue 3 T-weapp Target - 编译到微信小程序 V-3 Version - 3.x
Projects
None yet
Development

No branches or pull requests

3 participants