在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

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

vue中watch computed屬性,會(huì)有沖突嗎?

今天用了升級(jí)vux到2.7.5,發(fā)現(xiàn)里邊的previewer組件有bug,點(diǎn)擊圖片查看放大圖片,會(huì)提示Cannot read property 'w' of undefined錯(cuò)誤,看了里邊的實(shí)現(xiàn),發(fā)現(xiàn)watch了computed屬性imgs,imgs只返回了個(gè)空數(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計(jì)算出b,a變了,他就會(huì)重新計(jì)算,
watch 是你watch的那個(gè)東西變了會(huì)觸發(fā)的方法

2017年9月6日 01:08