Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript vue getter返回未定义_Javascript_Vue.js - Fatal编程技术网

Javascript vue getter返回未定义

Javascript vue getter返回未定义,javascript,vue.js,Javascript,Vue.js,有人能告诉我为什么这个getter返回我“无法读取未定义的属性'testVar'”吗 您使用的是this.testVar,但testVar在getter块中声明为常量,因此您必须将if条件声明为: productPrice: (state) => (id) => { const testVar = true if (testVar) { const priceObj = state.precioProducto.find(product => producto.

有人能告诉我为什么这个getter返回我“无法读取未定义的属性'testVar'”吗


您使用的是
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
}

非常感谢你,我已经试过了,但条件中的const出现了另一个错误,但我没有意识到const的名称是不同的
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
}