1.导入Echarts不能再cmd窗口直接导入,这样导入Vue项目无法查找到改包
可以用管理员打开cmd,然后跳转到项目所在的src目录下,运行一下代码,安装依赖
npm install echarts --save
或者运行下面代码下载制定版本
(通常Echarts5.0以上的版本会在后面使用echarts.init方法时报错“无法找到”,所以通常情况下导入4.9.0或者4.8.0)
npm install echarts@(“这里可以写版本号”)
2.然后在main.js文件中引入
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
3.开始使用
我在使用的时候直接把实例包装成方法,以下代码放到script标签内部,无语impprt
(this.$echarts就是调用全局变量)
export default {
name: "Course",
components: {
vHeader,
vSidebar,
},
data(){
return {
}
},
methods:{
picShow(data){
var myChart = this.$echarts.init(document.getElementById('main'));
var option = {
title: {
text: 'ECharts 入门示例'
},
tooltip: {},
legend: {
data:['销量']
},
xAxis: {
data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [
{
value: 30,
itemStyle: {
color: '#d906a0'
}
},
{
value: 30,
itemStyle: {
color: '#a90000'
}
},
36,
10,
10,
20]//展示的数据
}]
};
myChart.setOption(option)
},
mounted(){
},
watch:{
}
}
4.结果如下
![](https://i-blog.csdnimg.cn/blog_migrate/fa292ef181254998da53dff2d58f0eb5.png)