From dd71918d101bdb76cd40825ec02d3d8ee57d10fd Mon Sep 17 00:00:00 2001 From: Justineo Date: Fri, 30 Apr 2021 18:28:12 +0800 Subject: [PATCH] fix: make the `.chart` getter work with Vue 2 --- CHANGELOG.md | 1 + package.json | 2 +- src/ECharts.ts | 31 ++++++++++++++++--------------- src/composables/api.ts | 2 +- src/demo/Demo.vue | 2 -- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2030068..d9c7751 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * Changed `updateOptions.lazyUpdate` to `true` by default. ([#533](https://github.com/ecomfe/vue-echarts/issues/533#issuecomment-809883909)) * Only perform an additional `resize` call after init within a task. ([#533](https://github.com/ecomfe/vue-echarts/issues/533#issuecomment-809883909)) +* The `.chart` getter API now works for Vue 2. (#542) ## 6.0.0-rc.4 diff --git a/package.json b/package.json index 80d0355..c0f6131 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "vue-echarts", "description": "Vue.js component for Apache ECharts.", - "version": "6.0.0-rc.4", + "version": "6.0.0-rc.5", "repository": "https://github.com/ecomfe/vue-echarts.git", "license": "MIT", "author": "GU Yiling ", diff --git a/src/ECharts.ts b/src/ECharts.ts index 811c381..93511bb 100644 --- a/src/ECharts.ts +++ b/src/ECharts.ts @@ -14,8 +14,7 @@ import { nextTick, PropType, watchEffect, - Vue2, - getCurrentInstance + Vue2 } from "vue-demi"; import { init as initChart } from "echarts/core"; import { @@ -64,6 +63,9 @@ export default defineComponent({ ...loadingProps }, inheritAttrs: false, + created() { + console.log(this); + }, // eslint-disable-next-line @typescript-eslint/no-unused-vars // @ts-expect-error setup(props, { attrs, listeners }) { @@ -145,7 +147,16 @@ export default defineComponent({ function resize() { if (instance && !instance.isDisposed()) { - instance.resize(); + // temporarily suppress errors caused by https://github.com/apache/echarts/issues/14846 + try { + instance.resize(); + } catch (e) { + if (e.message === "Cannot read property 'get' of undefined") { + return; + } + + throw e; + } } } @@ -237,23 +248,13 @@ export default defineComponent({ onUnmounted(cleanup); - const chartGetter = { - get() { - return unref(chart); - } - }; - - const exposed = { + return { + chart, root, setOption, nonEventAttrs, ...publicApi }; - - Object.defineProperty(getCurrentInstance(), "chart", chartGetter); - Object.defineProperty(exposed, "chart", chartGetter); - - return exposed; }, render() { const attrs = { ...this.nonEventAttrs }; diff --git a/src/composables/api.ts b/src/composables/api.ts index 773e914..ea3d903 100644 --- a/src/composables/api.ts +++ b/src/composables/api.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ -import { Ref } from "vue-demi"; +import { Ref, unref } from "vue-demi"; import { EChartsType, Option } from "../types"; const METHOD_NAMES = [ diff --git a/src/demo/Demo.vue b/src/demo/Demo.vue index 4be76c4..40b28d0 100644 --- a/src/demo/Demo.vue +++ b/src/demo/Demo.vue @@ -557,8 +557,6 @@ export default { }, mounted() { this.startActions(); - - console.log(this.$refs.pie.chart); } };