解决echart官网引入方式报错问题,报错案例:
报错案例1:Initialize failed: invalid dom.
报错案例2:export 'default' (imported as 'echarts') was not found in 'echarts'
1.使用终端安装echart
npm install echarts --save
2、在vue项目中引入echarts
在 main.js 中添加下面两行代码($echarts可以自己设置,调用别名)
import * as echarts from 'echarts';
Vue.prototype.$echarts = echarts
3.创建一个新的vue文件,案例代码:
echart中,显示图标需要一个带宽度和高度的盒子。如下template的div。
//在Echarts.vue文件中
<template>
<div class="Echarts">
<div
id="main"
style="width: 600px;height: 400px;"
></div>
</div>
</template>
<script>
export default {
// eslint-disable-next-line vue/multi-word-component-names
name: 'Echarts',
methods: {
myEcharts () {
var myChart = this.$echarts.init(document.getElementById('main'));
//配置图表
var option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [
{
data: [150, 230, 224, 218, 135, 147, 260],
type: 'line'
}
]
};
myChart.setOption(option);
}
},
mounted () {
this.myEcharts();
}
}
</script>
<style>
</style>
示例图如图所示: