簡體   English   中英

vue getter 返回未定義

[英]vue getter return undefined

有人能告訴我為什么這個getter返回我“無法讀取未定義的屬性'testVar'”嗎?

productPrice: (state) => (id) => {
    const testVar = true
    if (this.testVar) {
        const priceObj = state.precioProducto.find(product => producto.idProducto === parseInt(id))
    } else {
        const priceObj = []
    }
    return (precioObj && state.productoPrecioReady) ? priceObj : null
},

您正在使用this.testVar ,但 testVar 在 getter 塊中被聲明為常量,因此您必須將if條件聲明為:

productPrice: (state) => (id) => {
  const testVar = true
  if (testVar) {
    const priceObj = state.precioProducto.find(product => producto.idProducto === parseInt(id))
  } else {
    const priceObj = []
  }
  return (precioObj && state.productoPrecioReady) ? priceObj : null
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM