vue中使用echarts(vue+vue-cli+axios+jsonp+echarts)

news/2024/7/9 23:44:06 标签: vue, echarts

一、安装echarts

cnpm i echarts -S

二、在vue-cli的main.js文件中引用echarts

import charts from 'echarts'
Vue.prototype.$echarts = charts

问题

引入 echars 5.0 遇到报错 "export ‘default’ (imported as ‘echarts’) was not found in ‘echarts

解决

引入方式改为:

import * as echarts from 'echarts';
// 或
const echarts = require('echarts');

三、Charts详细代码:

Charts.vue

<template>
  <div>
    <div id="myChart">

    </div>
  </div>
</template>

<script>
  export default {
    methods: {
      drawLine(){
        // 基于准备好的dom,初始化echarts实例
        let myChart = this.$echarts.init(document.getElementById('myChart'));

        this.$axios.get("http://127.0.0.1:8000/get_data")
          .then(function(res){
            // 绘制图表
            myChart.setOption({
              title: { text: res.data.title },
              tooltip: {},
              xAxis: {
                data: res.data.xAxisData
              },
              yAxis: {},
              series: [{
                name: '销量',
                type: 'bar',
                data: res.data.seriesData
              }]
            });
          })
          .catch(function(err){
            console.log(err);
          })
      }
    },
    mounted(){
      this.drawLine();
    }
  }
</script>

<style>
#myChart{
  height: 500px;
}
</style>

四、上面的图表数据通过axios获取,node.js代码如下:

let express = require("express");
let app = express();
 
app.get("/get_data", function(req, res, next){
	res.header("Access-Control-Allow-Origin", "*");
    let response = {
    	title: '在Vue中使用echarts',
    	xAxisData: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"],
    	seriesData: [10, 26, 16, 20, 16, 30]
    };
    res.type('application/json');
    res.jsonp(response);
});
 
app.listen(8000, function(){
    console.log("开始执行请求");
});

五、引用Charts.vue

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <router-view/>

    <Charts/>
  </div>
</template>

<script>
import Charts from './components/Charts.vue'
export default {
  name: 'App',
  components: {
    Charts
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

 


http://www.niftyadmin.cn/n/886881.html

相关文章

Vue-Route页面切换过渡动画

路由切换时有对应的组件加载区域(<router-view />)&#xff0c;在这个组件中添加如下CSS&#xff1a; .slide-fade{position: fixed;left:0;right: 0;width: 100%;background-color: white; } .slide-fade-enter, .slide-fade-leave-to {left:0;top: 0;right: 0;positio…

vue-router中如何让'router-link'渲染成指定的标签?

大家都知道vue-router中<router-link> 默认会被渲染成一个 <a> 标签&#xff1a; <script src"https://unpkg.com/vue/dist/vue.js"></script> <script src"https://unpkg.com/vue-router/dist/vue-router.js"></script…

Vue+Element-ui实现左侧二级导航(只展开一个菜单、可根据路由高亮菜单项、刷新时可自动展开定位到当前路由)

这里使用的Element文档版本是2.13.1。 路由文件index.js&#xff1a; import Vue from vue import Router from vue-router import Login from /components/Login import Main from /components/Main import Form from /components/Form import Data from /components/Data i…

ztree让整棵树一次只能选择一个复选框(节点)

<ul id"edit-mobile-disk-strategy-modal-host-group-tree" style"height: 200px;"></ul> //ztree配置 var setting {check : {enable : true,chkboxType : { "Y" : "", "N" : "" }},view : {dblCli…

vue登录权限实现(登录拦截)

用sessionStorage实现&#xff0c;文件夹结构如下&#xff1a; 关键部分如下&#xff1a; router.js import Vue from vue import Router from vue-router import Index from /components/index import Login from /components/login import Table from /components/table im…

NoSql相关概念介绍

NoSql相关概念介绍 什么是NoSQL NoSQL&#xff0c;是非关系型的数据库。NoSQL有时也称作Not Only SQL的缩写&#xff0c;是对不同于传统的关系型数据库的数据库管理系统的统称。 NoSQL用于超大规模数据的存储。些类型的数据存储不需要固定的模式&#xff0c;无需多余操作就可…

在vue2.0中使用bootstarp-table(jquery+bootstarp+bootstarpTable)

要想使用bootstarp-table就需要按顺序引入 jquery > bootstarp > bootstarp-table //路径可能会有变动 最好在node_modules 中看看 import $ from jquery import bootstrap/dist/css/bootstrap.min.css import bootstrap/dist/js/bootstrap.min.js import bootstrap-t…

MongoDB的特点及概念

MongoDB 的特点及概念 MongoDB 是一个介于关系数据库和非关系数据库之间的产品&#xff0c;是非关系数据库当中功能最丰富&#xff0c;最像关系数据库的。它是一个基于分布式文件存储的开源数据库系统。 在高负载的情况下&#xff0c;添加更多的节点&#xff0c;可以保证服务器…