Echarts Vue 组件

    1. 模板部分

      1. <template>
      2. <div class="chart-box">
      3. <div ref="echartsBox" class="echartsBox"></div>
      4. </div>
      5. </template>
    2. 行为部分

      1. export default {
      2. name: "",
      3. // 父组件数据 option 配置对象
      4. props: {
      5. option: {
      6. type: Object,
      7. default: () => {},
      8. },
      9. },
      10. data() {
      11. return {
      12. // 存储配置信息对象
      13. setOption : {}
      14. };
      15. },
      16. mounted() {
      17. // 接收 option 配置对象数据
      18. this.setOption = this.option
      19. // 调用初始化函数
      20. this.initChart()
      21. // 监听 / 重绘
      22. let chart = this.$echarts.init(this.$refs.echartsBox);
      23. window.addEventListener("resize", () => {
      24. chart.resize();
      25. }, true);
      26. },
      27. watch: {
      28. // 监听父组件传来数据
      29. option() {
      30. this.setOption = this.option
      31. this.initChart()
      32. }
      33. },
      34. methods: {
      35. // 初始化
      36. initChart (val) {
      37. let chart = this.$echarts.init(this.$refs.echartsBox);
      38. // 把配置和数据放这里
      39. chart.setOption(this.setOption)
      40. },
      41. // props 无效果可使用 - 获取 option 信息
      42. getOption(val){
      43. this.setOption = val
      44. this.initChart()
      45. }
      46. },
      47. };
    3. 样式部分

      1. .chart-box{
      2. width: 100%;
      3. height: 100%;
      4. .echartsBox{
      5. width: 100%;
      6. height: 100%;
      7. }
      8. }