大屏组件dataV使用踩坑记录

本文介绍了一种通过绑定key和监听窗口大小变化来确保视图实时更新的方法。具体实现了当配置改变时,通过更新组件内部的状态并增加key值来触发视图重新渲染,以此确保视图与最新配置保持同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 绑定key并在窗口大小改变后更改,避免更改配置后视图不更新,this.conf = { ...this.conf }也是避免视图不更新的。

<template>
  <div class="text-wrapper" :class="'text-line'" :style="config.style">
    <dv-percent-pond ref="chart" :key="key" :config="conf" :style="{width:config.style.width+'px',height:config.style.height+'px'}" />
  </div>
</template>

<script>
export default {
  name: 'PercentPond',
  props: {
    config: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      conf: {
        value: 0
      },
      key: 1
    }
  },
  computed: {
    tipsStyle() {
      return Object.assign({
        lineHeight: this.config.style.height - Number(this.config.style.borderWidth ? this.config.style.borderWidth.replace('px', '') : 0) * 2 - 10 + 'px'
      }, this.config.style)
    },
    style() {
      const style = {}
      if (Number(this.config.style.underline)) {
        style.textDecoration = 'underline'
      }
      if (this.config.style.fontSize) {
        style.fontSize = this.config.style.fontSize + 'px'
      }
      if (Number(this.config.style.weight)) {
        style.fontWeight = 'bold'
      }
      if (Number(this.config.style.style)) {
        style.fontStyle = 'italic'
      }
      if (Number(this.config.style.center)) {
        style.textAlign = 'center'
      }
      if (Number(this.config.style.verticalAlign)) {
        style.verticalAlign = 'middle'
      }
      return style
    }
  },
  watch: {
    'config': {
      handler() {
        this.change()
      },
      deep: true
    }
  },
  created() {
  },
  mounted() {
    this.change()
  },
  beforeDestroy() {
  },
  methods: {
    change() {
      if (this.config && this.config.params && this.config.params.result) {
        this.$set(this.conf, 'value', Number(this.config.params.result))
        this.conf = { ...this.conf }
        // 进度池配色
        if (this.config.style.startColor || this.config.style.endColor) {
          this.conf.colors = [this.config.style.startColor, this.config.style.endColor]
        } else {
          this.conf.colors = ['#3DE7C9', '#00BAFF']
        }
      }
      const that = this
      window.addEventListener('resize', function(e) {
        that.$nextTick(() => {
          that.key++
        })
      })

      // 此处为官网推荐更新数据方式
      /**
         状态更新
         避免你的组件更新了数据后,状态却不刷新,也就是没变化,请务必看这里
         组件props均未设置deep监听,刷新props时,请直接生成新的props对象(基础数据类型除外)
         ,或完
         成赋值操作后使用ES6拓展运算符生成新的props对象
         (this.someProps = { ...this.someProps })。
         **/
      // console.log(this.conf.colors, this.config.style)
    }
  }
}
</script>

<style scoped>
  .text-wrapper {
    width: 100%;
    height: 100%;
    display: table;
  }
  .text-body {
    display: table-cell;
  }
  .text-no-line {
    white-space:nowrap;
  }
  .text-line {
    word-break: break-all;
    word-wrap: break-word;
  }
  .text-wrapper p {
    margin: 0;
  }
  .text-tip {
    color: #888;
    width: 100%;
    height: 100%;
    text-align: center;
  }
  .over-hide {
    overflow: hidden;
  }
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值