鍍金池/ 問答/HTML5  HTML/ vue中watch computed屬性,會有沖突嗎?

vue中watch computed屬性,會有沖突嗎?

今天用了升級vux到2.7.5,發(fā)現(xiàn)里邊的previewer組件有bug,點擊圖片查看放大圖片,會提示Cannot read property 'w' of undefined錯誤,看了里邊的實現(xiàn),發(fā)現(xiàn)watch了computed屬性imgs,imgs只返回了個空數(shù)組。

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的那個東西變了會觸發(fā)的方法

2017年9月6日 01:08