vue v-for 循环出的标签 添加echarts 不显示

使用this.$nextTick方法 确保获取到dom 不使用可能标签dom没加载出来

this.$nextTick方法的讲解:5.7 Vue中this.$nextTick()方法的使用及代码示例-CSDN博客

我的代码

  <div class='chart'>
    <div class="card"
         v-for="(item, index) in dataList"
         :key="index">
      <div class="title">
        {{ item.name }}
      </div>
      <div class="content">
        <div class="content-item">
          <div class="item">
            <div class="count">
              {{ item.value1 }}
              <span>
                项
              </span>
            </div>
            <div class="unit">
              基表
            </div>
          </div>
          <div class="content-img">
            <div :class="'tableEcharts'+index"
                 style="width:100%; height:100%;"></div>
          </div>
        </div>
        <div class="content-item">
          <div class="item">
            <div class="count">
              {{ item.value2 }}
              <span>
                条
              </span>
            </div>
            <div class="unit">
              数据
            </div>
          </div>
          <div class="content-img">
            <div :class="'dataEcharts'+index"
                 style="width:100%; height:100%;"></div>
          </div>
        </div>
      </div>
    </div>
  </div>
  data() {
    return {
      dataList: [],
    }
  },  
mounted() {
    this.getData()
  },
  methods: {
    getData() {
      const url = '接口名'
      requestNetwork({}, url).then((res) => {
        if (res.Success) {
          this.dataList = res.Data
          console.log('JCSJFLTJ', this.dataList)
          this.dataList.forEach((item, index) => {
            item.value1 = item.value1
              .toString()
              .replace(/(?=\B(\d{3})+$)/g, ',')
            item.value2 = item.value2
              .toString()
              .replace(/(?=\B(\d{3})+$)/g, ',')
            this.tableEcharts(index, item.arr1)
          })
        }
      })
    },
    tableEcharts(index, arr) {
        // 关键点this.$nextTick 重新加载
      this.$nextTick(() => {
        var myChart = this.$echarts.init(
          document.querySelector('.tableEcharts' + index)
        )
        const bgColor = '#fff'
        const color = getColor() // 封装的颜色方法
        const hexToRgba = (hex, opacity) => {
          let rgbaColor = ''
          const reg = /^#[\da-f]{6}$/i
          if (reg.test(hex)) {
            rgbaColor = `rgba(${parseInt('0x' + hex.slice(1, 3))},${parseInt(
              '0x' + hex.slice(3, 5)
            )},${parseInt('0x' + hex.slice(5, 7))},${opacity})`
          }
          return rgbaColor
        }
        var option = {
          backgroundColor: bgColor,
          color: color,
          legend: {
            right: 10,
            top: 10,
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              show: false,
            },
          },
          grid: {
            top: '5%',
            left: '3%',
            right: '3%',
            bottom: '10%',
            containLabel: true,
          },
          xAxis: [
            {
              type: 'category',
              boundaryGap: false,
              axisLabel: {
                show: false,
              },
              axisLine: {
                show: false,
              },
              axisTick: {
                show: false,
              },
            },
          ],
          yAxis: [
            {
              type: 'value',
              name: '',
              axisLabel: {
                show: false,
              },
              splitLine: {
                show: false,
              },
              axisLine: {
                show: false,
              },
              axisTick: {
                show: false,
              },
            },
          ],
          series: [
            {
              name: '',
              type: 'line',
              smooth: true,
              showSymbol: false,
              symbolSize: 8,
              zlevel: 3,
              lineStyle: {
                normal: {
                  color: color[0],
                  shadowBlur: 3,
                  shadowColor: hexToRgba(color[0], 0.5),
                  shadowOffsetY: 8,
                },
              },
              areaStyle: {
                normal: {
                  color: new this.$echarts.graphic.LinearGradient(
                    0,
                    0,
                    0,
                    1,
                    [
                      {
                        offset: 0,
                        color: hexToRgba(color[0], 0.3),
                      },
                      {
                        offset: 1,
                        color: hexToRgba(color[0], 0.1),
                      },
                    ],
                    false
                  ),
                  shadowColor: hexToRgba(color[0], 0.1),
                  shadowBlur: 10,
                },
              },
              data: arr,
            },
          ],
        }
        myChart.setOption(option)
      })
    },
}

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值