请问一下this指向的问题,this已经重定义,还是不行

先贴上代码

this.myChart = echarts.init(document.querySelector('.heat .main'))
this.myChart.setOption({
  legend: {},
  tooltip: {
      trigger: 'axis',
      showContent: false
  },
  dataset: {
    source: [
      ['product', '2012', '2013', '2014', '2015', '2016', '2017'],
      ['Matcha Latte', 41.1, 30.4, 65.1, 53.3, 83.8, 98.7],
      ['Milk Tea', 86.5, 92.1, 85.7, 83.1, 73.4, 55.1],
      ['Cheese Cocoa', 24.1, 67.2, 79.5, 86.4, 65.2, 82.5],
      ['Walnut Brownie', 55.2, 67.1, 69.2, 72.4, 53.9, 39.1]
    ]
  },
  xAxis: {type: 'category'},
  yAxis: {gridIndex: 0},
  grid: {top: '55%'},
  series: [
    {type: 'line', smooth: true, seriesLayoutBy: 'row'},
    {type: 'line', smooth: true, seriesLayoutBy: 'row'},
    {type: 'line', smooth: true, seriesLayoutBy: 'row'},
    {type: 'line', smooth: true, seriesLayoutBy: 'row'},
    {
      type: 'pie',
      id: 'pie',
      radius: '30%',
      center: ['50%', '25%'],
      label: {
          formatter: '{b}: {@2012} ({d}%)'
      },
      encode: {
        itemName: 'product',
        value: '2012',
        tooltip: '2012'
      }
    }
  ]
});
this.myChart.on('updateAxisPointer', function (event) {
  let that = this;
  let xAxisInfo = event.axesInfo[0];
  if (xAxisInfo) {
    let dimension = xAxisInfo.value + 1;
    that.myChart.setOption({
      series: {
        id: 'pie',
        label: {
          formatter: '{b}: {@[' + dimension + ']} ({d}%)'
        },
        encode: {
          value: dimension,
          tooltip: dimension
        }
      }
    });
  }
});

错误是报在this.myChart.on(),这个方法里,在重新setOption时,报了这个错误image.png,请问这是为什么呢?

阅读 1.7k
2 个回答
let that = this;
this.myChart.on('updateAxisPointer', function (event) {

写在外面

都用上ES6了为什么不直接用箭头函数的语法

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题