今天用了升级vux到2.7.5,发现里边的previewer组件有bug,点击图片查看放大图片,会提示Cannot read property 'w' of undefined错误,看了里边的实现,发现watch了computed属性imgs,imgs只返回了个空数组。
computed: {
imgs () {
return this.list.map(one => {
if (!one.msrc) {
one.msrc = one.src
}
if (typeof one.w === 'undefined') {
one.w = 0
one.h = 0
}
return one
})
}
},
watch: {
imgs (newVal, oldVal) {
if (!this.photoswipe) {
return
}
if (newVal.length && newVal.length - oldVal.length === -1) {
const index = this.photoswipe.getCurrentIndex()
this.photoswipe.invalidateCurrItems()
this.photoswipe.items.splice(index, 1)
let goToIndex = index
if (goToIndex > this.photoswipe.items.length - 1) {
goToIndex = 0
}
this.photoswipe.goTo(goToIndex)
this.photoswipe.updateSize(true)
this.photoswipe.ui.update()
} else if (!newVal.length) {
this.close()
}
}
}
computed 是你用a计算出b,a变了,他就会重新计算,
watch 是你watch的那个东西变了会触发的方法