echarts 中 init 步骤需要获取 dom 元素,但 vue3 中 setup 在 beforeCreate 和 created 之间执行,此时还没有 dom 元素。因此选择在 mounted 钩子中进行初始化
将配置项 options 设置成 reactive,并用 watch 侦听其数据变化
let options = reactive({
...
});
let myChart;
onMounted(() => {
myChart = echarts.init(document.getElementById("step-chart"));
myChart.setOption(options);
});
watch(options, (newVal) => myChart.setOption(newVal));