vue 引入 echarts 子组件

一:创建 以.vue 结尾的 文件,也就是 子组件

如下,这是一个完整 的可以引用 一个 echarts 图标  的vue子组件。 我们对以下进行分析

几乎 引用 所有的 echarts 图标都可以用这个方式。 

<template>
// :class 绑定的是类名 :stle 绑定了它的宽度和高度, 这里width 和height 可以是自己 默认的 (比如下面的width ),也可以是 父组件传过来的, 比如下面的height <div :class="className" :style="{height:this.chartData.chartHeight,width:width}" /> </template> <script>
// 这里是单页面引用echarts ,优点是 哪里用哪里 引用。 也可以全局引用,自行百度 import echarts from 'echarts' require('echarts/theme/macarons') // echarts theme export default {
// props 这里是一些默认参数,定义了默认的echarts的样式,上面 的 class 和 style 中的没有定义的变量会从这里找 props: { className: { type: String, default: 'chart' }, width: { type: String, default: '100%' }, height: { type: String, default: '400px' }, autoResize: { type: Boolean, default: true }, chartData: { type: Object, required: true } }, data() { return { chart: null, } },
// 这里是当 数据 chartData 比较复杂时,需要用到深度监视, 这里参考博客 https://www.cnblogs.com/yesu/p/9546458.html 和 https://www.cnblogs.com/wsz168/p/8763284.html
// 我自己的思考, 这里 深度监听chartData ,并且把最新的数据复制个 setOptions这个方法
watch: { chartData: { deep: true, handler(val) { this.setOptions(val) } } },
// 如果要获取数据更新导致的DOM更新后的新DOM对象需要使用$nextTick回调 ,在dom对象 加载完执行 图标初始化 mounted() { this.$nextTick(() => { this.initChart() }) },
// 在 页面 销毁前执行 beforeDestroy() { if (!this.chart) { return } this.chart.dispose(); this.chart = null }, //方法 methods: { initChart() { this.chart = echarts.init(this.$el, 'macarons') this.setOptions(this.chartData) }, setOptions({ dataList, 1,2,3} = {}) { this.chart.setOption({ title: { text: '三大平台七日新增条数', left: 'center', textStyle:{ // 设置标题文字大小 fontSize:18, align:'center', //水平对齐 color:'black' }, }, xAxis: { data: dataList, boundaryGap: false, axisTick: { show: false } }, grid: { left: 10, right: 20, bottom: 20, top: 60, containLabel: true }, tooltip: { trigger: 'axis', axisPointer: { type: 'cross' }, padding: [5, 10] }, yAxis: { axisTick: { show: false } }, legend: { data: ['1新增条数', '2新增条数','3新增条数'], padding:[40,50,0,0], }, series: [{ name: '2新增条数', itemStyle: { normal: { color: '#FF005A', lineStyle: { color: '#FF005A', width: 2 } } }, smooth: true, type: 'line', data: 111, animationDuration: 2800, animationEasing: 'cubicInOut' }, { name: '2新增条数', smooth: true, type: 'line', itemStyle: { normal: { color: '#3888fa', lineStyle: { color: '#3888fa', width: 2 }, areaStyle: { color: '#f3f8ff' }} }, data: 222, animationDuration: 2800, animationEasing: 'quadraticOut' }, { name: '1新增条数', smooth: true, type: 'line', itemStyle: { normal: { color: '#FF7E00', lineStyle: { color: '#FF7E00', width: 2 }, areaStyle: { color: '#f3f8ff' }} }, data: 333, animationDuration: 2800, animationEasing: 'quadraticOut' }] }) } } } </script> <style scoped> </style>

  注意: 此时引入组建后 浏览器会报错 , 报错原因: 

  1. 在 Vue 中,初始化的值不会被 watch 钩子捕捉,从而导致组件被调用方调用并赋予 option 参数时不会进入校验。虽然可以使用 immediate: true 使得 watch 钩子能够在属性初始化赋值时被触发,但这样做是不合适的。因为这样设置之后,在 option 初始化从而触发 watch 时,用于挂载 echarts 的 DOM 元素还未存在于页面中,从而导致出现 TypeError: Cannot read property 'setOption' of null 的错误。我们要重点注意 echarts 作用的生命周期,这一点后续还会涉及。

         详情在:  https://blog.csdn.net/weixin_33852020/article/details/88972608

  这里我的解决办法是不用 watch监测,把wacth去掉,在mounted方法中  this.initChart() 使用如下代码代替

  

        var that = this;
        setTimeout(function() {
          that.initChart()
        },200)

  

 

posted @ 2020-10-30 15:42  zzzzzyyyyyy  阅读(1538)  评论(0编辑  收藏  举报