echarts绑定点击事件,在点击事件里实现vue-router跳转

507 阅读1分钟

1、这个涉及到了this的指向,我是把echarts写成vue插件用的,不是在vue里直接引用的。
插件写法:
建一个echarts.js
// 引入这两个插件的前提是,你要npm安装一下哈,
import echarts from 'echarts'
//这个插件是用来实现,echarts图表跟据父容器大小自适应的
import ElementResizeDetectorMaker from 'element-resize-detector'
const echartsInstance = function (Vue) { Vue.prototype.$erd = ElementResizeDetectorMaker()

//Object.defineProperties用这个方法给Vue原型上定义方法,这个方法不会被实         //例对象改变。我刻是这么说的哈,错了再改吧。
Object.defineProperties(Vue.prototype, {
    $echartsPlugin: {
        get () {
            let that = this //重点哈,这里将指向vue的this保存在了that
            return {
            getGraph(id){
                let chart =   echarts.init(document.getElementById(id))
                chart.on('click',function(params) {
                       // 因为我写的吧,是关系图,所以我要判断一下是点击到节点了,还是点到关系边上了
                        if (params.dataType === 'node'){
                            // 是节点我要让他跳vue的路邮了
                            that.$router.push({
                            path:'/你在router.js里配置的',
                            query:{
                                你要传的参数。
                            }
                            })
                        }
                    })
                    let potion = {
                        //你的配置
                    }
                    //setOption第二个设置了一个true是说第一次都要重绘echart图标,在折线图里多条线往一条线上切换时,会有问题,设置这个也能解决。
                  chart.setOption(option,true)
                  that.$erd.listenTo(
                   document.getElementById(id),
                   function () {
                       that.$nextTick(funcion(){
                       chart.resize()
                       })
                   }
                  )
            }
                    
                    
            }
        }
    }
})

}