【问题标题】:Reactive Vue Chart.js component using Typescript使用 Typescript 的反应式 Vue Chart.js 组件
【发布时间】:2020-01-29 15:30:39
【问题描述】:

使用vue-property-decorator(或实际上是Componentvue-class-component 中的Prop)和vue-chartjs-typescript,我正在尝试使用属性chartDataoptions 制作BarChart 组件。以下代码在为应用提供服务时确实有效:

<script lang="ts">
  import { Bar, mixins } from 'vue-chartjs-typescript'
  import Vue from 'vue'
  import { Component, Prop } from 'vue-property-decorator'
  const { reactiveProp } = mixins

  @Component({
    extends: Bar,
    mixins: [reactiveProp],
  })
  export default class BarChart extends Vue {
    @Prop()
    chartData: any

    @Prop({default: function () { return {} }})
    options!: object

    mounted() {
      this.renderChart(this.chartData, this.options)
    }
  }
</script>

但是,在构建应用程序时它会失败。我的 IDE(PyCharm Pro)也给出了同样的错误:

TS2339:“BarChart”类型上不存在属性“renderChart”。

因此,当使用@Component({extends: Bar}) 时,对于解释器来说,不清楚该组件是否扩展了Bar。我已经尝试扩展Bar 而不是Vue,所以export default class BarChart extends Bar。但这会产生以下错误:

TypeError: Super expression 必须为 null 或函数

任何想法如何解决这个问题?

【问题讨论】:

    标签: typescript vuejs2 chart.js vue-class-components


    【解决方案1】:

    你可以在BarChart类中显式定义你想使用的函数

      public renderChart!: (chartData: any, options: any) => void
    

    【讨论】:

      猜你喜欢
      • 2020-03-29
      • 2019-01-08
      • 2019-06-13
      • 2019-09-18
      • 2020-01-09
      • 2018-02-20
      • 2022-11-29
      • 2020-12-15
      • 2020-12-01
      相关资源
      最近更新 更多