zoukankan      html  css  js  c++  java
  • vue 2.0 全局引入eCharts以及按需引用eCharts

    echarts 官方文档中的“在webpack中使用eCharts”  链接 http://echarts.baidu.com/tutorial.html#%E5%9C%A8%20webpack%20%E4%B8%AD%E4%BD%BF%E7%94%A8%20ECharts

    在所需项目中安装完了之后:

    全局引用

    main.js 中配置

    import echarts from 'echarts'  //引入echarts
    
    Vue.prototype.$echarts = echarts  //注册组件

    eCharts.vue 中

    <div id="myChart" :style="{ '300px', height: '300px'}"></div>
    <script>
    export default {
        name: 'eCharts',
        data () {
            return {
            msg: 'Welcome to Your Vue.js App'
            }
        },
        mounted(){
            this.drawLine();
        },
        methods: {
            drawLine(){
                // 基于准备好的dom,初始化echarts实例
                var myChart = this.$echarts.init(document.getElementById('myChart'))
                // 绘制图表
                myChart.setOption({
                    title: { text: '在Vue中使用echarts' },
                    tooltip: {},
                    xAxis: {
                        data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
                    },
                    yAxis: {},
                    series: [{
                        name: '销量',
                        type: 'bar',
                        data: [5, 20, 36, 10, 10, 20]
                    }]
                });
            }
        }
    }
    </script>

    按需引用

    这时就不需要在main.js中进行配置了

    eCharts.vue

    html 不变

    然后在 script 引入自己需要的模块,如下代码 (更多模块有: https://github.com/ecomfe/echarts/blob/master/index.js

    <script>
    // 引入基本模板
    // let echarts = require('echarts/lib/echarts')
    var echarts = require('echarts')
    // 引入柱状图组件
    require('echarts/lib/chart/bar')
    // 引入提示框和title组件
    require('echarts/lib/component/tooltip')
    require('echarts/lib/component/title')
    export default {
        name: 'eCharts',
        data () {
            return {
            msg: 'Welcome to Your Vue.js App'
            }
        },
        mounted(){
            this.drawLine();
        },
        methods: {
            drawLine(){
                // 基于准备好的dom,初始化echarts实例
                var myChart = echarts.init(document.getElementById('myChart'))
                // 绘制图表
                myChart.setOption({
                    title: { text: '在Vue中使用echarts' },
                    tooltip: {},
                    xAxis: {
                        data: ["衬衫","羊毛衫","雪纺衫","裤子","高跟鞋","袜子"]
                    },
                    yAxis: {},
                    series: [{
                        name: '销量',
                        type: 'bar',
                        data: [5, 20, 36, 10, 10, 20]
                    }]
                });
            }
        }
    }
    </script>

    注:这里用 requir 不用 import 引入是因为 import 需要详细的路径

    另外 

    var myChart = echarts.init(document.getElementById('myChart')) 
    按需引用的时候将 this.$echarts 改为 echarts
  • 相关阅读:
    ES6~Promise的原理及使用二 理解Promise规范(copy自:https://www.cnblogs.com/lvdabao/p/5320705.html?utm_source=tuicool&utm_medium=referral)
    Google的Python代码格式化工具YAPF详解
    在ubuntu上使用QQ的经历
    Ubuntu 14.04 下安装Skype
    pip install lxml mysql-python error
    情人节的宠物-测试小工具
    戴维营收集
    工作日志(10.29)
    求职面试
    面试题集(转载)
  • 原文地址:https://www.cnblogs.com/xll-qg/p/8489480.html
Copyright © 2011-2022 走看看