如何在vue项目中引入不同版本的echarts(引入2个不同的版本)???

1,812 阅读1分钟

问题现象:

在项目开发过程中,有时候不得不引入2个echarts版本,因为一些例子比如散点图,使用echarts5才能实现,echarts4里面实现不了,但是echarts5中对其他的例子兼容性又不好,怎么办呢?

1.引入2个不同的版本如下图package.json文件中:

"echarts4": "npm:echarts@^4.9.0",
"echarts5": "npm:echarts@^5.0.2",

image.png

2.使用如下图

echarts4和echarts5引入方式注意区分:

  import echarts4 from 'echarts4'
  import * as echarts5 from 'echarts5'

image.png

  that.lineChart2 = echarts4.init(document.getElementById("tgTimeContent-l-b-content-canvas"));
  that.lineChart3 = echarts5.init(document.getElementById("tgTimeContent-r-t-content-canvas"));
  that.lineChart4 = echarts4.init(document.getElementById("tgTimeContent-r-b-content-canvas"));
  window.addEventListener("resize", () => {
        that.lineChart2.resize();
        that.lineChart3.resize();
        that.lineChart4.resize();
  });

image.png