Argument of type 'Vue | Element

2020-05-29  本文已影响0人  草帽lufei

vue2.6 , typescript3 项目引入 Echarts 时报错,内容如下

Argument of type 'Vue | Element | Vue[] | Element[]' is not assignable to parameter of type 'HTMLDivElement | HTMLCanvasElement'.
  Type 'Vue' is not assignable to type 'HTMLDivElement | HTMLCanvasElement'.
    Type 'Vue' is missing the following properties from type 'HTMLCanvasElement': height, width, getContext, toBlob, and 243 more.Vetur(2345)

ECharts.vue

<template>
  <div>
    <div ref="main" style="width:500px;height:500px;"></div>
  </div>
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
import * as echarts from 'echarts'
@Component({
  components: {}
})
export default class ECharts extends Vue {
  private mounted() {
    console.info('mounted')
    console.info(echarts)
    console.info(this.$refs.main)
    const mainEle = this.$refs.main
    const myChart = echarts.init(mainEle)
    myChart.setOption({
      title: {
        text: 'ECharts 入门示例'
      },
      tooltip: {},
      xAxis: {
        data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
      },
      yAxis: {},
      series: [
        {
          name: '销量',
          type: 'bar',
          data: [5, 20, 36, 10, 10, 20]
        }
      ]
    })
    console.info(myChart)
  }
}
</script>
<style scoped lang="scss"></style>

错误出现在 ECharts.vue 第17行 const mainEle = this.$refs.main

解决方案

this.$refs.main 内容修改为 this.$refs.main as HTMLDivElement 即可

上一篇下一篇

猜你喜欢

热点阅读