echarts二级页面展示响应式 和 resize事件报错

如果是vue2中这样写

// 基于准备好的dom,初始化echarts实例
const myCharts = echarts.init(document.getElementById('echart1))
myCharts.clear() // 看情况 不加好像也没事
window.addEventListener('resize', function (){
  //自适应屏幕
  myCharts.resize() 
});
window.dispatchEvent(new Event('resize')); 
// 这样就实现了二级页面图表的自适应 图表样式宽度为百分百

我目前用的react 图表用的echarts-for-react组件

import ReactEcharts from 'echarts-for-react';
   componentDidMount() {
        const { series } = this.props;
        const option = {
            title: {
                // text: '项目收支时间曲线',
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                }
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            toolbox: {
                feature: {
                    // saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                /*  boundaryGap: false, */
                data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月']
            },
            yAxis: {
                type: 'value'
            },
            series
        };
        this.setState({ option });
        window.addEventListener('resize', this.suit);
        window.dispatchEvent(new Event('resize'));
    }
    suit = () => {
        const myCharts = this.echartRef;
        myCharts && myCharts.resize(); 
        // myCharts.resize() 直接这样写 二次进入该页面会报错
 // Cannot read properties of null (reading 'getAttribute') 
    };

    componentWillUnmount() {
        this.echartRef = null;
        window.removeEventListener('resize', this.suit, 20);
    }

我是这样解决的! 直接myCharts.resize() ; 第一次进页面的时候不会错误 但是再次点击就会报错

Cannot read properties of null (reading 'getAttribute') 所以我是进行了逻辑中断

myCharts && myCharts.resize();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值