【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 (()=>{//获取数据}) 包裹一下,但是本人没试过

但是上述方法都无法解决我的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
  }

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

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

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

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

结果发现: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')

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

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

扫描二维码关注公众号,回复: 14471083 查看本文章
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
    }
}

此时页面数据已返回

猜你喜欢

转载自blog.csdn.net/weixin_52834435/article/details/126301236