• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

echarts怎么引入到vue中?

vue 搞代码 4年前 (2021-12-26) 81次浏览 已收录 0个评论

echarts怎么引入到vue中?下面本篇文章就来给大家介绍一下vue项目中引用echarts的几种方式。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

准备工作:

首先我们初始化一个vue项目,执行vue init webpack echart。接着我们进入初始化的项目下,安装echarts,

npm install echarts -S 
//或
cnpm install echarts -S

安装完成之后,我们就可以开始引入我们需要的echarts了,接下来介绍几种使用echarts的方式。

全局引用:

首先在main.js中引入echarts,将其绑定到vue原型上:

  1. import echarts from 'echarts'

  2. Vue.prototype.$echarts = echarts;

接着,我们就可以在任何一个组件中使用echarts了,接下来我们在初始化项目中的helloWorld组件中使用echarts配置图标,具体如下:

<template>
    <div>
         <div style="width:500px;height:500px" ref="chart"></div>
    </div>
</template>
<script>
export default{
   data () {
  return {};
 },
 methods: {
  initCharts () {
  let myChart = this.$echarts.init(this.$refs.chart);
  console.log(this.$refs.chart)
  // 绘制图表
  myChart.setOption({
  title: { text: '在Vue中使用echarts' },
  tooltip: {},
  xAxis: {
  data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
  },
  yAxis: {},
  series: [{
  name: '销量',
  type: 'bar',
  data: [5, 20, 36, 10, 10, 20]
  }]
  });
  }
   },
   mounted () {
  this.initCharts();
 }
}
</script>

这样下来,就可以在项目的任何地方使用echarts了。

局部使用:

当然,很多时候没必要在全局引入ecahrts,那么我们只在单个组件内使用即可,代码更加简单:

<template>
<div>
<div style="width:500px;height:500px" ref="char<p style="color:transparent">来源gao!daima.com搞$代!码网</p>t"></div>
</div>
</template>
<script>
const echarts = require('echarts');
export default{
data () {
return {};
},
methods: {
initCharts () {
let myChart = echarts.init(this.$refs.chart);
// 绘制图表
myChart.setOption({
title: { text: '在Vue中使用echarts' },
tooltip: {},
xAxis: {
data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10, 20]
}]
});
}
},
mounted () {
this.initCharts();
}
}
</script>

可以看到,我们直接在组件内引入echarts,接下来跟全局引入的使用一样。区别在于,这种方式如果你想在其他组件内用echarts,则必须重新引入了。

更多vue.js相关知识,可访问 Vue.js答疑 栏目!!

以上就是echarts怎么引入到vue中?的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:echarts怎么引入到vue中?

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址