时间:2023-01-11 10:46:13 | 栏目:vue | 点击:次
在vue开发中,因为引用的父组件和子组件都使用了window.onresize以至于一个window.onresize失效。
可以采用下面的方式
window.onresize = () => this.screenWidth = window.innerWidth
// 改为以下写法
window.addEventListener('resize', () => this.screenWidth = window.innerWidth, false)
多个子组件中都存在window.onresize时,后一个会把前一个覆盖,导致之前的onresize都失效。
const _this = this
window.onresize = function() {
if (_this.chart) {
_this.chart.resize()
}
}
使用addEventListener方法添加监听
const _this = this
window.addEventListener('resize', () => {
if (_this.chart) {
_this.chart.resize()
}
})