vue中watch computed属性,会有冲突吗?

今天用了升级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()
  }
}

}

阅读 2.8k
1 个回答

computed 是你用a计算出b,a变了,他就会重新计算,
watch 是你watch的那个东西变了会触发的方法

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题