跳转至

报错:未获取到context

问题

mp.runtime.esm.js?3240:613 [Vue warn]: Error in onLoad hook: "Error: [uCharts] 未获取到context!注意:v2.0版本后,需要自行获取canvas的绘图上下文并传入opts.context!"

修改前

methods: {
  // 圆环图 
  showRing(canvasId, chartData) {
    canvaRing = new uCharts({
      $this: _self,
      canvasId: canvasId,
      type: 'ring',
      fontSize: 11,
      legend: true,
      extra: {
        ring: {
          offsetAngle: -45,
          ringWidth: 30 * _self.pixelRatio,
          lableWidth: 150,
          centerColor: '#000000'
        }
      },
      background: '#FFFFFF',
      pixelRatio: _self.pixelRatio,
      series: _self.Ring.series,
      animation: true,
      width: _self.cWidth * _self.pixelRatio,
      height: _self.cHeight * _self.pixelRatio,
      disablePieStroke: true,
      dataLabel: true,
    });
  },
}

解决

context:uni.createCanvasContext(canvasId, _self) 
//canvasId 画布的id   _self 全局this

修改后

methods: {
  // 圆环图 
  showRing(canvasId, chartData) {
    canvaRing = new uCharts({
      //$this: _self,
      //canvasId: canvasId,
      context:uni.createCanvasContext(canvasId, _self) 
      type: 'ring',
      fontSize: 11,
      legend: true,
      extra: {
        ring: {
          offsetAngle: -45,
          ringWidth: 30 * _self.pixelRatio,
          lableWidth: 150,
          centerColor: '#000000'
        }
      },
      background: '#FFFFFF',
      pixelRatio: _self.pixelRatio,
      series: _self.Ring.series,
      animation: true,
      width: _self.cWidth * _self.pixelRatio,
      height: _self.cHeight * _self.pixelRatio,
      disablePieStroke: true,
      dataLabel: true,
    });
  },
}