合理解决vue封装的Echarts重复渲染不显示(vue2,3通用)

项目场景:

项目场景:在通过vue封装Echarts组件,对其进行重复使用。


问题描述

在重复使用封装的Echarts组件时至显示一个组件


原因分析:

大致原因是因为Echart封装的时候直接操作的dom是绑定的为id通过document.getElementById(‘idName’)来实现dom获取的,但是我们都忘记了一个特性,id是唯一的,重复使用的时候是否会造成冲突呢?

1、因此我尝试着使用class来获取dom结果显而易见Echarts官方可能不允许,直接报获取dom语法错误
2、我有尝试着用vue的ref获取,同样不行,看来只能用id
方法:我们只需要保证复用的时候id不一样就行了,因此如下操作


解决方案:

1、用一个统一的div带有class属性的包裹echarts需要操作的div

<template>
	<!-- 外壳div -->
    <div class="box">
    	<!-- 渲染echarts div -->
        <div></div>
    </div>
</template>

2、动态给这个div生成id(通过父传子,prors传入动态id)

const props = defineProps({
    id: {
      type: String,
      default: "c1"
    },
    index: {
      type: Number,
      default: 0
    },
    color: {
      type: String,
      default: 'red'
    },
    data: {
      type: Array,
      default: () => [120, 132, 101, 134, 90, 230, 210]
    },
    xAxisData :{
        type: Array,
        default: () => ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    }

3、动态获取dom就行了

      const box = document.getElementsByClassName('box');
      // 动态生成 id(不唯一),注意,这里的props.index 是你的包裹div (box) 的序号,复用的时候会产生多个box,需要index进行定位
      box[props.index].childNodes[0].id = props.id
     // 动态获取dom
      const chartDom = document.getElementById(props.id);
      // 注意!! 一定要加 宽和高
      chartDom.style.height='100%'
      const myMiniLineChart = $echarts.init(chartDom);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 封装 Echarts 的方法: 1. 安装 Echarts 使用 npm 安装 Echarts: ``` npm install echarts --save ``` 2. 创建 Vue 组件 创建一个 Vue 组件,例如 `Echarts.vue`。 3. 引入 Echarts 在 `Echarts.vue` 中引入 Echarts: ``` import echarts from 'echarts' ``` 4. 初始化 Echarts 在 `mounted` 钩子函数中初始化 Echarts: ``` mounted() { this.initChart() }, methods: { initChart() { const container = this.$refs.container this.chart = echarts.init(container) } } ``` 5. 配置 Echarts 在 `initChart` 方法中配置 Echarts: ``` initChart() { const container = this.$refs.container this.chart = echarts.init(container) const option = { // 配置项 } this.chart.setOption(option) } ``` 6. 监听组件大小变化 为了保证 Echarts 图表在组件大小变化时能够自适应,需要监听组件大小变化,并重新渲染 Echarts 图表。 ``` mounted() { this.initChart() window.addEventListener('resize', this.resizeHandler) }, beforeDestroy() { window.removeEventListener('resize', this.resizeHandler) }, methods: { resizeHandler() { this.chart.resize() } } ``` 7. 封装成可复用的组件 将以上步骤封装成一个可复用的 Vue 组件,供其他组件使用。例如: ``` <template> <div ref="container" style="width: 100%; height: 300px;"></div> </template> <script> import echarts from 'echarts' export default { name: 'Echarts', props: { option: { type: Object, required: true } }, mounted() { this.initChart() window.addEventListener('resize', this.resizeHandler) }, beforeDestroy() { window.removeEventListener('resize', this.resizeHandler) }, methods: { initChart() { const container = this.$refs.container this.chart = echarts.init(container) this.chart.setOption(this.option) }, resizeHandler() { this.chart.resize() } } } </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值