【问题标题】:Cannot include high-charts in vue component不能在 vue 组件中包含高位图表
【发布时间】:2019-07-01 10:18:14
【问题描述】:

您好,我正在尝试在组件内的 vue cli 代码中包含 highcharts。但我收到“未知的自定义元素:-您是否正确注册了组件?”对于递归组件,请确保在控制台中提供“名称”选项。

我已经使用以下命令安装了 highcharts

npm install --save highcharts

npm install highcharts-vue

我做错了什么或遗漏了什么吗?

Home.vue

<template>
<div class="col-md-9 chartDiv">
  <h4 align='center'>{{ tableChart.title2 }}</h4>
  <div class="col-md-12">
    {{tableChart.chartOption}}
    <div class="chart-size center-block">
      <highcharts name="highcharts" v-bind:options="tableChart.chartOption" 
        ref="highcharts" charttype="highchart"></highcharts>
    </div>
  </div>
</div>
</template>

<script>
export default {
name: 'HomePage',
props: {
tableChart: {
  type: Object,
  required: false
}
},
data: function() {
return this.tableChart;
 }
};
</script>

App.vue

<template>
<div id="app">
<home-page v-bind:table-chart="tableData"></home-page>
</div>
</template>
<script>
import HomePage from './components/HomePage.vue';
import tableData2018 from './dataSource.js';
export default {
 name: 'app',
 components: {
   HomePage
 },
 data: function() {
  return {
    years: [2018, 2019, 2020],
    tableData: {}
  }
 },
methods: {
 initReport: function() {
  this.tableData = JSON.parse(tableData2018);
  }
},
created: function() {
  this.initReport();
}
};

main.js

import Vue from 'vue';
import App from './App.vue';

Vue.config.productionTip = false;

new Vue({
 render: h => h(App),
 }).$mount('#app');



dataSource.js
{"chartOption":{"chart":{"plotBorderWidth":1,"type":"bubble","zoomType":"xy"},"credits":{"enabled":true,"href":"#","position":{"align":"right","verticalAlign":"bottom","y":-10},"text":"www.highcharts.com"},"exporting":{"enabled":false,"filename":"exportChart","type":"image/svg+xml","url":"http://export.highcharts.com.","width":600},"legend":{"align":"center","enabled":0,"floating":false,"layout":"vertical","verticalAlign":"bottom","x":0,"y":0,"z":0},"plotOptions":{"series":{"dataLabels":{"enabled":true,"format":"{point.name}"}}},"series":[{"data":[{"description":"Name","name":"xcode","x":1.02,"y":577039,"z":4092}]}],"title":{"text":""},"tooltip":{"followPointer":false,"footerFormat":"</table>","headerFormat":"<table>","pointFormat":"<tr><thcolspan=2><h5>{point.description}</h5></th></tr><tr><th>AdjustedReadmissions/ExpectedReadmissions:</th><td>{point.x}</td></tr><tr><th>ExpectedPenalty:</th><td>${point.y}</td></tr><tr><th>Index Admissions:</th><td>{point.z}</td></tr>","useHTML":1},"xAxis":{"gridLineWidth":1,"labels":{"format":"{value}"},"plotLines":[{"color":"black","dashStyle":"dot","value":1,"width":2,"zIndex":3}],"tickmarkPlacement":"on","title":{"text":"AdjustedReadmissions/ExpectedReadmissions"}},"yAxis":{"labels":{"format":"{value}"},"showFirstLabel":false,"title":{"text":"ExpectedPenalty"}}}}

【问题讨论】:

    标签: javascript vue.js highcharts vuejs2 vue-component


    【解决方案1】:

    在您的 main.js 文件中,您应该导入 highcharts-vue 并在调用 new Vue() 之前加载它

    ma​​in.js:

    import Vue from "vue";
    import App from "./App";
    import HighchartsVue from "highcharts-vue";
    
    Vue.config.productionTip = false;
    
    Vue.use(HighchartsVue);
    
    new Vue({
      el: "#app",
      components: { App },
      template: "<App/>"
    });
    

    演示:

    【讨论】:

    • 谢谢,它有效。我试图在 highcharts 中包含气泡图,但收到错误“挂载钩子中的错误:“错误:Highcharts 错误 #17:www.highcharts.com/errors/17”。它说“Highcharts 错误 #17 请求的系列类型确实不存在 当您将 chart.type 或 series.type 设置为 Highcharts 中未定义的系列类型时,会发生此错误。一个典型的原因可能是您缺少定义系列类型的扩展文件,例如,为了运行 arearange 系列,您需要加载 highcharts-more.js 文件。”。如何克服这个
    • 你必须加载highcharts-more 模块并初始化它。文档:github.com/highcharts/…。导入:import HighchartsMore from 'highcharts/highcharts-more' 并初始化:HighchartsMore(Highcharts);。如果您觉得我的回答有帮助,请点赞,谢谢。
    猜你喜欢
    • 2018-05-07
    • 2019-09-02
    • 2021-08-09
    • 1970-01-01
    • 2018-05-29
    • 2018-02-12
    • 2021-09-30
    • 2015-08-19
    • 1970-01-01
    相关资源
    最近更新 更多