chart接入后台数据后vue不响应式显示图片

Posted sinceforever

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了chart接入后台数据后vue不响应式显示图片相关的知识,希望对你有一定的参考价值。

chart接入后台数据后vue不响应式显示图片

watch: {
  //观察option的变化
  config: {
    handler(newVal, oldVal) {
      if (this.chart) {
        if (newVal) {
          this.chart.setOption(newVal);
        } else {
          this.chart.setOption(oldVal);
        }
      } else {
        this.init();

      }
    },
    deep: true //对象内部属性的监听,关键。
  }
  },

完整的charts文件内容,我是自己写的公用组件,

  • Charts.vue
<template>
  <div class="pr chart">
    <div ref="chart" class="chart-body"></div>
    <slot></slot>
  </div>
</template>

<script scoped>
export default {
  props: {
    config: {
      type: Object,
      default: () => ({})
    },

    onClick: {
      type: Function,
      default: () => {}
    },
    onDispatchAction: {
      type: Object,
      default: () => ({})
    },
     onMouseover: {
      type: Function,
      default: () => {}
    }
  },
  watch: {
    //观察option的变化
    config: {
      handler(newVal, oldVal) {
        if (this.chart) {
          if (newVal) {
            this.chart.setOption(newVal);
          } else {
            this.chart.setOption(oldVal);
          }
        } else {
          this.init();

        }
      },
      deep: true //对象内部属性的监听,关键。
    }
  },
  mounted() {
    if (!this.$echarts) {
      return;
    }
    //初始化echarts
    let echartsWarp = this.$refs["chart"];
    let chart = (this.chart = this.$echarts.init(echartsWarp));
    chart.setOption(this.config);

    //点击旋转扇形,切该扇形高亮,我用的是每次点击重新渲染图,
    //这样可以每次计算扇形起点,达到旋转的视觉效果
    chart.on("click", params => {
      this.onClick(params);
      chart.setOption(this.config);
    });
    //高亮设置
    chart.dispatchAction(this.onDispatchAction)
    // //初始化设置高亮
    chart.dispatchAction({
      type: ‘highlight‘,
      seriesIndex: 0,
      dataIndex: 0
    })
    //取消鼠标进入自动高亮效果
    chart.on("mouseover", params => {
      this.onMouseover(params);
    });
    //屏幕大小改变时,图形大小自适应
    window.addEventListener("resize", () => {
      chart.resize();
    });
  },
};
</script>
<style scoped>
.chart {
  height: 3.7rem;
  width: 3.7rem;
  margin: 0 auto;
}
.chart-body {
  width: 100%;
  height: 100%;
}
</style>

以上是关于chart接入后台数据后vue不响应式显示图片的主要内容,如果未能解决你的问题,请参考以下文章

Vue模板语法——数据响应式

探索 Vue.js 响应式原理

vue Object.freeze之打破双向绑定 让对象不响应式更新

甘特图[javascript甘特图,甘特图组件,vue甘特图,vue Gantt图表,响应式甘特图,项目经理,vue项目]

在codeigniter中带有tinymce的响应式文件管理器不显示缩略图

前端批量上传图片后端怎么接收?