Vue.js 3.2.1下的Gauge.js组件未创建

时间:2018-12-11 22:13:13

标签: vuejs2

“嗨,我正在尝试让一个简单的gauge.js在Vue.js v3.2.1下工作。文档引用get总是返回null,以使装入的函数永远不会完成。这应该很简单,但是我担心我会因为Vue.js陌生而在某个地方缺少知识,并且找不到任何帮助。”

<template >
    <canvas ref="foo"></canvas>
</template>

<script>
import { Gauge } from 'gauge.js'

export default 
{
name: 'vcGaugeJs',
props: 
{
    value: {type: Number, default: 0},
    //options: { type: Object, default: () => ({}) }
},

data() 
{
      return 
  {
       gauge: null
      }
},

mounted: function () 
{   
this.initGauge();
},
  watch: 
  {
    value: function (val) 
{   
    this.gauge.set(val);
    },
  },



updated: function()
{
if (this.gauge == null)
{
    this.initGauge();
}
 },



  methods:
{
    initGauge () 
    {
        let opts = 
    {
        angle: 0, // The span of the gauge arc
        lineWidth: 0.35, // The line thickness
        radiusScale: 1, // Relative radius
        pointer: 
        {
            length: 0.53, // // Relative to gauge radius
            strokeWidth: 0.057, // The thickness
            color: '#000000' // Fill color
        },
        limitMax: false,     // If false, max value increases automatically if value > maxValue
        limitMin: false,     // If true, the min value of the gauge will be fixed
        colorStart: '#6F6EA0',   // Colors
        colorStop: '#C0C0DB',    // just experiment with them
        strokeColor: '#EEEEEE',  // to see which ones work best for you
        generateGradient: true,
        highDpiSupport: true     // High resolution support
    }

    var target = this.$refs.foo;
    if (target == null)
    {
        console.log ("Null target ref!");
    }
    else
    {
        this.gauge = new Gauge(this.$refs.foo);
        this.gauge.maxValue = 3000; // set max gauge value
        this.gauge.setMinValue(0);  // Prefer setter over gauge.minValue = 0
        this.gauge.animationSpeed = 62; // set animation speed (32 is default value)
        this.gauge.set(1700); // set actual value
        this.gauge.setOptions(opts); // create gauge!
    }
}
},  

}    

   

1 个答案:

答案 0 :(得分:0)

the dev team的意思是:

  

如果您查看lifecycle digram,则可以看到在调用created()钩子时,尚未编译组件的模板/渲染功能。

因此,在您的情况下,应该可以在mounted上的vm.$el钩子处实例化量规,而将其作为画布元素。

mounted() {
   this.initGauge();
},

methods: {
   initGauge() {
      let opts = { /* options */}

      this.gauge = new Gauge(this.$el).setOptions(opts);

      this.gauge.maxValue = 3000; // set max gauge value
      this.gauge.setMinValue(0);  // Prefer setter over gauge.minValue = 0
      this.gauge.animationSpeed = 62; // set animation speed (32 is default value)
      this.gauge.set(1700); // set actual value
   }
}