在項目中的兩個需求是輪播圖中需要出現不同的echart圖表且要視配大小中屏,出現警告框時左邊的echart圖表要跟著變小,警告框消失時,需要將echart圖表相應變大
此時,我的項目中,echart圖表都是寫在子組件,
在子組件中,我寫的echart图表视屏相应如下
mounted() {
this.DataProcess();
window.addEventListener(‘resize’, () => {
this.
e
c
h
a
r
t
s
.
i
n
i
t
(
t
h
i
s
.
echarts.init(this.
echarts.init(this.refs.myChart).resize();
});
},
activated() {
window.addEventListener(‘resize’, () => {
this.
e
c
h
a
r
t
s
.
i
n
i
t
(
t
h
i
s
.
echarts.init(this.
echarts.init(this.refs.myChart).resize();
});
},
beforeDestroy() {
window.removeEventListener(‘resize’, () => {
this.
e
c
h
a
r
t
s
.
i
n
i
t
(
t
h
i
s
.
echarts.init(this.
echarts.init(this.refs.myChart).resize();
});
this.
e
c
h
a
r
t
s
.
d
i
s
p
o
s
e
(
t
h
i
s
.
echarts.dispose(this.
echarts.dispose(this.refs.myChart);
},
在父组件中的屏幕大小改变时,并没有触发子组件中的echart图表宽度大小的改变。
我想的就是通过.style来操控echart图表宽度大小,但是都没有用,
最后还是看到了原先的代码this.
e
c
h
a
r
t
s
.
d
i
s
p
o
s
e
(
t
h
i
s
.
echarts.dispose(this.
echarts.dispose(this.refs.myChart);
发现父组件发生改变时,并没有触发该函数的调用。
在父组件出现警告框的同时,写一个标识告诉子组件父组件触发了,子组件再监听该标识
如下,则触发了该函数,响应父组件而改变echart图表宽度大小。
watch: {
show1(newVal,oldVal){
console.log(‘值’,newVal);
this.
e
c
h
a
r
t
s
.
i
n
i
t
(
t
h
i
s
.
echarts.init(this.
echarts.init(this.refs.myChart).resize();
}
},
相对应的是,轮播图也是如此,监听父元素的屏幕大小改变,但是该监听更加频繁。
用ElementUI可以很好的解决该轮播,但是样式却发生了些许改变。
于是我用原生js写轮播,(三个图表要显示在一个盒子里,一个一个的轮播,考虑子元素少于三个和大于三个),例如:
Echarts視配大小中屏
于 2023-04-03 15:40:50 首次发布
文章描述了一个Vue项目中遇到的问题,即在轮播图中嵌入Echart图表并要求图表随窗口大小和警告框出现/消失动态调整。解决方案包括在子组件中监听父组件的状态变化,通过watcher调整图表大小,并使用原生JS实现轮播图功能,处理子元素数量小于或大于三个的情况。
摘要由CSDN通过智能技术生成