使用 Typescript 的 Reactive Vue Chart.js 组件

Nem*_*man 6 typescript chart.js vuejs2 vue-class-components

使用VUE属性-装饰(或实际上其ComponentPropVUE级组分)和VUE-chartjs-打字稿,我试图让BarChart与性-componentchartDataoptions。以下代码在为应用程序提供服务时实际上有效:

<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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

类型错误:超级表达式必须为空或函数

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

小智 5

您可以在类中显式定义要使用的函数BarChart

  public renderChart!: (chartData: any, options: any) => void
Run Code Online (Sandbox Code Playgroud)