Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist. vue.min.js:6 TypeError: Cannot read properties of null (reading 'nickname')

1.问题描述

在html调用indexUser.nickname的属性时,没重新启动项目直接查阅该index.html,出现了页面加载出来的一片空白的情况,去掉{{indexUser.nickname}}页面就正常
image

2.问题分析

这种情况是访问不到值(未定义或为空)

3.解决方案

写了一小段检查是否为未定义或者空的代码,随便在html新建一个button,绑定check事件,check()写在script的methond里

点击查看代码
check(){
                if (typeof this.indexUser !== 'undefined') {
                    alert("未定义");
                    console.log("未定义")
                }
                if (this.indexUser == null ) {
                    alert("为空");
                    console.log("为空")
                }
            }

1.先确认拼写无错误
2.确认一下有没有定义
image
3.如果是后台取的数据,注意不要用直接打开html的方法,这时候值可能没取到,比如我获取值是从this.indexUser=JSON.parse(localStorage.getItem('indexUser'));前端缓存获取的,这个值又是从login.html取得后再跳转到Index页面,这时候才正式得到indexUser的值
总结一下:如果你的值需要其他页面的辅助,这时候你直接打开就会因为值还没有进去而报空,可以完整走一遍项目看看是不是还是报空

posted @ 2024-12-13 12:00  momo996  阅读(2331)  评论(0)    收藏  举报