echarts is not defined

关于echarts is not defined
在vue中使用echarts的柱状图时遇到echarts is not defined 可能是因为路径的问题
解决办法:
1 把 color: new echarts.graphic.LinearGradient 改成 color: new this.echarts.graphic.LinearGradient
2 在当前项目的当前页面中引入 import echarts from "echarts"

附:
echarts 在vue3中的使用方法
1 npm install echarts -S
2 在mian.js中引入
2.1 import echarts from "echarts"
2.2 Vue.use(echarts)
2.3 Vue.prototype.$echarts = echarts

完整代码示例:

<template>  <div class="box">    <div class="header">      <div class="dataCollection">数据补录</div>      <div class="dataSite">数据补录</div>      <div class="dataTable">数据补录</div>      <div class="dataUpdata">数据补录</div>    </div>    <div class="conent">      <div class="right"></div>      <div class="left">        <div id="myChart" :style="{width: '400px', height: '400px'}"></div>      </div>    </div>    <div class="footer">      <div id="lineChart" :style="{width: '100%', height: '100%'}"></div>    </div>  </div></template>
  <script>import echarts from "echarts"export default {  name: "hello",  data() {    return {      msg: "Welcome to Your Vue.js App",          };  },  mounted() {    this.drawLine();    this.lineCharts();  },  methods: {    drawLine() {      // 基于准备好的dom,初始化echarts实例      let myChart = this.$echarts.init(document.getElementById("myChart"));      // 绘制图表      myChart.      setOption({        title: {          text: "南宁(234567)更新于.......",          left: "center",        },        series: [          {            name: "访问来源",            type: "pie",            radius: "55%",            center: ["50%", "60%"],            data: [              { value: 335, name: "直接访问" },              { value: 310, name: "邮件营销" },              { value: 234, name: "联盟广告" },              { value: 135, name: "视频广告" },              { value: 1548, name: "搜索引擎" },            ],            emphasis: {              itemStyle: {                shadowBlur: 10,                shadowOffsetX: 0,                shadowColor: "rgba(0, 0, 0, 0.5)",              },            },          },        ],      });    },   lineCharts() {    var dataAxis = ['点', '击', '柱', '子', '或', '者', '两', '指', '在', '触', '屏', '上', '滑', '动', '能', '够', '自', '动', '缩', '放'];    var data = [220, 182, 191, 234, 290, 330, 310, 123, 442, 321, 90, 149, 210, 122, 133, 334, 198, 123, 125, 220];    var yMax = 500;    var dataShadow = [];    for (var i = 0; i < data.length; i++) {        dataShadow.push(yMax);    }    let lineChart = this.$echarts.init(document.getElementById("lineChart"));      // 绘制图表     lineChart.      setOption({       title: {        text: '特性示例:渐变色 阴影 点击缩放',        subtext: 'Feature Sample: Gradient Color, Shadow, Click Zoom'    },    xAxis: {        data: dataAxis,        axisLabel: {            inside: true,            textStyle: {                color: '#fff'            }        },        axisTick: {            show: false        },        axisLine: {            show: false        },        z: 10    },    yAxis: {        axisLine: {            show: false        },        axisTick: {            show: false        },        axisLabel: {            textStyle: {                color: '#999'            }        }    },    dataZoom: [        {            type: 'inside'        }    ],    series: [        { // For shadow            type: 'bar',            itemStyle: {                color: 'rgba(0,0,0,0.05)'            },            barGap: '-100%',            barCategoryGap: '40%',            data: dataShadow,            animation: false        },        {            type: 'bar',            itemStyle: {                color: new echarts.graphic.LinearGradient(                    0, 0, 0, 1,                    [                        {offset: 0, color: '#83bff6'},                        {offset: 0.5, color: '#188df0'},                        {offset: 1, color: '#188df0'}                    ]                )            },            emphasis: {                itemStyle: {                    color: new echarts.graphic.LinearGradient(                        0, 0, 0, 1,                        [                            {offset: 0, color: '#2378f7'},                            {offset: 0.7, color: '#2378f7'},                            {offset: 1, color: '#83bff6'}                        ]                    )                }            },            data: data        }    ]            });    },},
};</script>
<style lang="scss" scoped>.box {  display: flex;  flex-direction: column;  .header {    height: 20%;    border: 1px solid red;    display: flex;    .dataCollection {      height: 100px;      width: 120px;      background-color: rgb(51, 255, 0);    }    .dataSite {      height: 100px;      width: 400px;      background-color: rgb(105, 105, 56);    }    .dataTable {      height: 100px;      width: 400px;      background-color: rgb(92, 92, 66);    }    .dataUpdata {      height: 100px;      width: 400px;      background-color: rgb(236, 236, 215);    }  }  .conent {    height: 520px;    border: 1px solid black;    display: flex;    .right {      flex-grow: 15;      border: 1px solid black;    }    .left {      flex-grow: 1;      border: 1px solid black;      display: flex;      justify-content: center;    }  }  .footer {    height: 170px;    border: 1px solid rgb(4, 19, 228);  }}</style>

在这里插入图片描述

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值