• 【vue3项目】解决 “TypeError: Cannot read properties of undefined (reading ‘xxx‘)“


    解决 “TypeError: Cannot read properties of undefined (reading ‘xxx’)”

    这个问题,我在网上搜了好久,网上总结的bug的原因如下:

    //这个报错 我的问题是 要用到的数据读不到这个属性(我用的vue)
    //1.检查你的data定义的属性是不是没有你用到的这个属性,没有的话就定义一个,如下:
    #template
    <div class="he-info__item">
        <span class="he-label">收货人姓名:</span>
        <span class="he-value">{{ detail.buyer.name }}</span>
    </div>
    <div class="he-info__item">
       <span class="he-label">联系方式:</span>
       <span class="he-value">{{ detail.buyer.mobile }}</span>
    </div>
    
    #js
    export default {
       data () {
           detail: {
            buyer: {
              name: "",
              mobile: "",
            },
            user: {
              nickname: "",
            },
          },
       }
    }
    
    //2.也可能是后端返回给你的数据没有这个属性 或者 返回的有的有数据 有的是 null ,
    // 这时候就不能写 {{ item.xxx || “” }} 不然会报错 Cannot read properties of undefined (reading ‘xxx‘)“ 可以这么解决 如下:
    #template
     <div v-if="!!item.invite">{{ item.invite.nickname }}</div> //有这个属性才显示   
     //或者这样也行
     <div v-if="item?.invite">{{ item.invite.nickname }}</div> //有这个属性才显示
    
    
     <div v-else>{{ "" }}</div> //没有返回 或者 null 直接填 “”
    
    
    //3.网上还有一种就是 视图未更新 数据还没返回 你就开始使用这个属性 可以加个 this.$nectTick (()=>{//获取数据}) 包裹一下,但是本人没试过
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    但是上述方法都无法解决我的bug,

    我的相关代码片段:
    定义一个 getUserList 函数,并且在 onMounted 函数中挂载它,在加载页面时,先通过我自定义的已全局挂载的$api向后端发送请求,获取列表数据

    import { onMounted, reactive, ref, getCurrentInstance } from 'vue'
    
    
    // 下面是setup中的代码
      const { ctx } = getCurrentInstance()//这个是获取vue3中的this指向,因为在vue3中没有this
     	
      onMounted(() => {
        getUserList()
      })
      const getUserList = async () => {
        let params = { ...user, ...pager }
        const { list, page } = await ctx.$api.getUserList(params)
        userList.value = list
        pager.total = page.total
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    逻辑上没有问题,同时也没有出现上述的三种情况。

    我的解决方法:
    首先这个报错的意思大概是:无法找到 undefined 的 属性。
    那在我的代码中的意思就是:找不到 ctx.$api,也就是说 ctx.$api是undefined。

    然后我在控制台中打印了一些值:

    console.log('ctx: ', ctx)
    console.log('ctx.$api: ', ctx.$api) //undefined
    
    • 1
    • 2

    结果发现:ctx.$api 为 undefined,并且 ctx 身上并没有我们绑定的 $api

    所以,我猜测是 $api 全局绑定没有生效

    为了验证,我又在全局对象上绑定了一些东西,并进行测试:

    // 在main.js中
    app.config.globalProperties.$user = {
      name: '梅长苏',
      weapons: '长剑',
      title: '刺客'
    }
    
    //在自己的页面中使用:
    console.log(ctx.$user)// undefined
    
    console.log(ctx.$user.name) //TypeError: Cannot read properties of undefined (reading 'name')
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    所以就验证了bug 的原因是:vue3的全局绑定没有生效

    所以,我在该页面进行了绑定:

    import { onMounted, reactive, ref, getCurrentInstance } from 'vue'
    import api from './../api'
    
    setup(){
    	onMounted(() => {
          getUserList()
        })
        const getUserList = async () => {
          // console.log('ctx: ', ctx)
          // console.log('ctx.$api: ', ctx.$api)
          ctx.$api = api //进行局部绑定,生效!!!
          let params = { ...user, ...pager }
          const { list, page } = await ctx.$api.getUserList(params)
          console.log('ctx.$api 成功绑定')
          userList.value = list
          pager.total = page.total
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    此时页面数据已返回

  • 相关阅读:
    kotlin基础教程:<5>集合与数组
    服务器硬件得基础知识介绍
    运动耳机什么牌子的好,推荐几款排行靠前的耳机
    Vue computed/watch原理
    新南威尔士大学研究团队延长量子相干时间实现基准增长100倍
    嵌入式分享合集104
    java中的clone方法
    【附源码】Python计算机毕业设计数据时代下的疫情管理系统
    leetcode:6151. 统计特殊整数【数位dp模板】
    python判断是否到了文件尾
  • 原文地址:https://blog.csdn.net/weixin_52834435/article/details/126301236