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> |