首页 > 其他分享 >使用计算Vuex得出的数据,后台报错Cannot read properties of undefined (reading 'XXX')

使用计算Vuex得出的数据,后台报错Cannot read properties of undefined (reading 'XXX')

时间:2023-02-08 21:11:23浏览次数:40  
标签:undefined read state Cannot 报错 计算 数据 categoryView

Vue控制台报错

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'category1Name')

 先说明我这个情况:我用计算属性返回了state里面的数据,并且在页面挂载时发请求获取数据,展示计算数据时,虽然程序正常运行,但是控制台报错

 <span >{{categoryView.category1Name}}</span>

const categoryView= computed(()=>{
          return store.state.detail.details.categoryView
        })

 mounted(){
      store.dispatch('getDetail',this.route.params.goodId)
    }

  分析:它说'category1Name'没被定义,可是我页面正常展示并且数据也是存在的,所以这个报错肯定是在数据展示之前就有,是个假错误。
    但是它确实有段时间没被定义或者说不存在,那就是数据还没返回到Vuex或者还没被计算出时,就已经展现到了页面上,导致计算的数据返回为空,因此内部元素也就没被定义了

  解决:提前给计算的属性返回一个非空即可

const categoryView = computed(()=>{
          return store.state.detail.details.categoryView || {}
        })

 

  


      

 

标签:undefined,read,state,Cannot,报错,计算,数据,categoryView
From: https://www.cnblogs.com/bjpfee/p/17103297.html

相关文章