吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 807|回复: 2
收起左侧

[求助] JS,EChart+ElementPlus的使用问题

  [复制链接]
cqwcns 发表于 2023-2-17 17:14
我希望在ElementPlus的drawer组件中渲染EChart图表。


如果图表容器在drawer组件外则没有问题,所以应该是由于drawer组件在文档初始化时并不在dom节点,EChart无法渲染。
我不知道如何解决,请各位大佬指教,感谢。


以下是一个简单的测试demo。

[JavaScript] 纯文本查看 复制代码
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试</title>

    <!-- style -->
    <link rel="stylesheet" href="https://candyissupersweet.gitee.io/cdn/element-plus/element.plus.index.css" />
    <!-- Vue 3 -->
    <script src="https://candyissupersweet.gitee.io/cdn/vue3/vue.global.prod.js"></script>
    <!-- component library -->
    <script src="https://candyissupersweet.gitee.io/cdn/element-plus/element.plus.full.min.js"></script>
    <!-- echarts -->
    <script type="text/javascript" src="https://candyissupersweet.gitee.io/cdn/echarts/echarts.min.js"></script>
</head>

<body>
    <div id="app" style="width: 100vw;height: 100vh;">

        <el-button type="primary" @click="doOpen">打开</el-button>

        <!-- 如果图表容器在drawer外,则可以正常渲染图表 -->
        <!-- <div id="container" style="width: 100%;height: 100%;"></div> -->

        <el-drawer v-model="drawer" title="图表" direction="btt" size="100%">
            <!-- 如果图表容器在drawer内,则渲染图表失败 -->
            <div id="container" style="width: 100%;height: 100%;"></div>
        </el-drawer>

    </div>

    <script>
        let that = null, App = null, myChart = null;


        App = {
            data() {
                return {
                    drawer: false
                }
            },
            mounted() {
                that = this;

            },
            methods: {
                doOpen() {
                    console.log('开始');

                    // 先显示容器
                    that.drawer = true;

                    // 如果没有初始化,就进行初始化
                    if (!myChart) myChart = echarts.init(document.getElementById('container'));

                    const option = {
                        xAxis: {
                            type: 'category',
                            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
                        },
                        yAxis: {
                            type: 'value'
                        },
                        series: [
                            {
                                data: [820, 932, 901, 934, 1290, 1330, 1320],
                                type: 'line',
                                smooth: true
                            }
                        ]
                    };
                    myChart.setOption(option);
                }
            }
        }

        const app = Vue.createApp(App);
        app.use(ElementPlus);
        app.mount("#app");
    </script>
</body>

</html>

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

notproblem 发表于 2023-2-17 17:23
[HTML] 纯文本查看 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
 
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试</title>
 
    <!-- style -->
    <link rel="stylesheet" href="https://candyissupersweet.gitee.io/cdn/element-plus/element.plus.index.css" />
    <!-- Vue 3 -->
    <script src="https://candyissupersweet.gitee.io/cdn/vue3/vue.global.prod.js"></script>
    <!-- component library -->
    <script src="https://candyissupersweet.gitee.io/cdn/element-plus/element.plus.full.min.js"></script>
    <!-- echarts -->
    <script type="text/javascript" src="https://candyissupersweet.gitee.io/cdn/echarts/echarts.min.js"></script>
</head>
 
<body>
    <div id="app" style="width: 100vw;height: 100vh;">
 
        <el-button type="primary" @click="doOpen">打开</el-button>
 
        <!-- 如果图表容器在drawer外,则可以正常渲染图表 -->
<!--        <div id="container" style="width: 100%;height: 100%;"></div> -->
 
        <el-drawer v-model="drawer" title="图表" direction="btt" size="100%">
            <!-- 如果图表容器在drawer内,则渲染图表失败 -->
            <div id="container" style="width: 100%;height: 100%;"></div>
        </el-drawer>
 
    </div>
 
    <script>
        let that = null, App = null, myChart = null;
 
 
        App = {
            data() {
                return {
                    drawer: false
                }
            },
            mounted() {
                that = this;
 
            },
            methods: {
                doOpen() {
                    console.log('开始');
 
                    // 先显示容器
                    that.drawer = true;
 
                    // 如果没有初始化,就进行初始化
					this.$nextTick(() => {
						if (!myChart) myChart = echarts.init(document.getElementById('container'));
						 
						const option = {
						    xAxis: {
						        type: 'category',
						        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
						    },
						    yAxis: {
						        type: 'value'
						    },
						    series: [
						        {
						            data: [820, 932, 901, 934, 1290, 1330, 1320],
						            type: 'line',
						            smooth: true
						        }
						    ]
						};
						myChart.setOption(option);
					})
                }
            }
        }
 
        const app = Vue.createApp(App);
        app.use(ElementPlus);
        app.mount("#app");
    </script>
</body>
 
</html>

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
cqwcns + 1 + 1 谢谢@Thanks!

查看全部评分

imyxuan 发表于 2023-2-17 19:34
Drawer 的内容是懒渲染的,即在第一次被打开之前,传入的默认 slot 不会被渲染到 DOM 上。 因此,如果需要执行 DOM 操作,或通过 ref 获取相应组件,请在 open 事件回调中进行。

因此,不要在点击按钮的时候进行表格初始化操作,在el-drawer的open事件中执行就好。
[HTML] 纯文本查看 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
 
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>测试</title>
 
    <!-- style -->
    <link rel="stylesheet"  />
    <!-- Vue 3 -->
    <script src="https://candyissupersweet.gitee.io/cdn/vue3/vue.global.prod.js"></script>
    <!-- component library -->
    <script src="https://candyissupersweet.gitee.io/cdn/element-plus/element.plus.full.min.js"></script>
    <!-- echarts -->
    <script type="text/javascript" src="https://candyissupersweet.gitee.io/cdn/echarts/echarts.min.js"></script>
</head>
 
<body>
    <div id="app" style="width: 100vw;height: 100vh;">
 
        <el-button type="primary" @click="doOpen">打开</el-button>
 
        <!-- 如果图表容器在drawer外,则可以正常渲染图表 -->
        <!-- <div id="container" style="width: 100%;height: 100%;"></div> -->
 
        <el-drawer v-model="drawer" title="图表" direction="btt" size="100%" @open="onOpen">
            <!-- 如果图表容器在drawer内,则渲染图表失败 -->
            <div id="container" style="width: 100%;height: 100%;"></div>
        </el-drawer>
 
    </div>
 
    <script>
        let that = null, App = null, myChart = null;
 
 
        App = {
            data() {
                return {
                    drawer: false
                }
            },
            mounted() {
                that = this;
 
            },
            methods: {
                doOpen() {
                    console.log('开始');
 
                    // 先显示容器
                    that.drawer = true;
                    
                },
                onOpen() {
                    if (!myChart) myChart = echarts.init(document.getElementById('container'));
 
                    const option = {
                        xAxis: {
                            type: 'category',
                            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
                        },
                        yAxis: {
                            type: 'value'
                        },
                        series: [
                            {
                                data: [820, 932, 901, 934, 1290, 1330, 1320],
                                type: 'line',
                                smooth: true
                            }
                        ]
                    };
                    myChart.setOption(option);
                },
            }
        }
 
        const app = Vue.createApp(App);
        app.use(ElementPlus);
        app.mount("#app");
    </script>
</body>
 
</html>

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
cqwcns + 1 + 1 谢谢@Thanks!

查看全部评分

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 02:04

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表