fix: chart instance should always be initialized

This commit is contained in:
Justineo
2022-01-18 16:55:13 +08:00
parent 677f100b07
commit 899ef4b8d5
4 changed files with 18 additions and 14 deletions

View File

@ -1,3 +1,8 @@
## 6.0.2
* Make `notMerge` option still respect `update-options`.
* The default behavior of `notMerge` now revert to checking if there is a reference change for the `option` prop.
## 6.0.1 ## 6.0.1
* Update should always be `notMerge: true`. * Update should always be `notMerge: true`.

View File

@ -80,7 +80,7 @@ export default defineComponent({
const { autoresize, manualUpdate, loading, loadingOptions } = toRefs(props); const { autoresize, manualUpdate, loading, loadingOptions } = toRefs(props);
const realOption = computed( const realOption = computed(
() => manualOption.value || props.option || Object.create(null) () => manualOption.value || props.option || null
); );
const realTheme = computed(() => props.theme || unref(defaultTheme) || {}); const realTheme = computed(() => props.theme || unref(defaultTheme) || {});
const realInitOptions = computed( const realInitOptions = computed(
@ -92,7 +92,7 @@ export default defineComponent({
const nonEventAttrs = computed(() => omitOn(attrs)); const nonEventAttrs = computed(() => omitOn(attrs));
function init(option?: Option) { function init(option?: Option) {
if (chart.value || !root.value) { if (!root.value) {
return; return;
} }
@ -141,7 +141,10 @@ export default defineComponent({
} }
function commit() { function commit() {
instance.setOption(option || realOption.value, realUpdateOptions.value); const opt = option || realOption.value;
if (opt) {
instance.setOption(opt, realUpdateOptions.value);
}
} }
if (autoresize.value) { if (autoresize.value) {
@ -226,16 +229,14 @@ export default defineComponent({
} }
}); });
const publicApi = usePublicAPI(chart, init); const publicApi = usePublicAPI(chart);
useLoading(chart, loading, loadingOptions); useLoading(chart, loading, loadingOptions);
useAutoresize(chart, autoresize, root); useAutoresize(chart, autoresize, root);
onMounted(() => { onMounted(() => {
if (props.option) { init();
init();
}
}); });
onUnmounted(cleanup); onUnmounted(cleanup);

View File

@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
import { Ref } from "vue-demi"; import { Ref } from "vue-demi";
import { EChartsType, Option } from "../types"; import { EChartsType } from "../types";
const METHOD_NAMES = [ const METHOD_NAMES = [
"getWidth", "getWidth",
@ -24,17 +24,12 @@ type MethodName = typeof METHOD_NAMES[number];
type PublicMethods = Pick<EChartsType, MethodName>; type PublicMethods = Pick<EChartsType, MethodName>;
export function usePublicAPI( export function usePublicAPI(
chart: Ref<EChartsType | undefined>, chart: Ref<EChartsType | undefined>
init: (option?: Option) => void
): PublicMethods { ): PublicMethods {
function makePublicMethod<T extends MethodName>( function makePublicMethod<T extends MethodName>(
name: T name: T
): (...args: Parameters<EChartsType[T]>) => ReturnType<EChartsType[T]> { ): (...args: Parameters<EChartsType[T]>) => ReturnType<EChartsType[T]> {
return (...args) => { return (...args) => {
if (!chart.value) {
init();
}
if (!chart.value) { if (!chart.value) {
throw new Error("ECharts is not initialized yet."); throw new Error("ECharts is not initialized yet.");
} }

View File

@ -590,6 +590,9 @@ export default {
}, },
mounted() { mounted() {
this.startActions(); this.startActions();
},
beforeUnmount() {
this.stopActions();
} }
}; };
</script> </script>