当前位置:网站首页>Error in render: “TypeError: Cannot read properties of null (reading ‘0‘)“ 报错解决方案
Error in render: “TypeError: Cannot read properties of null (reading ‘0‘)“ 报错解决方案
2022-08-02 03:22:00 【小茹想睡觉】
近期在写项目的时候遇到一个很有趣的问题,当我准备获取一个数组中的子数组到页面时,提示我报错Error in render: “TypeError: Cannot read properties of null (reading ‘0’)”,渲染的时候不能读取,这是怎么回事呢,赶紧搜索一下,发现问题了
这是我原来报错的代码
<view class="apply-name">
<text>{
{
item.applicant }}</text>
<text>
{
{
item.inspector[0] }}
</text>
</view>
报错信息
出现报错的原因
我们在调用inspector这个接口的时候,是异步调用,而在vue的渲染机制中,异步数据先显示初始数据(也就是本来就存在的数据),再显示带数据的数据,所以当vue开始执行的时候,inspector 中是没有数据的,所以才会报错。
解决方法
解决方法也很简单,就是在加载该片段的时候添加一个判断语句,v-if=“item.inspector” 如果它为空,就不进行该模块的渲染,更改后代码如下
<view class="apply-name">
<text>{
{
item.applicant }}</text>
<text v-if="item.inspector">
{
{
item.inspector[0] }}
</text>
</view>
解决是解决了,但是为什么v-if作用就这么厉害呢,是不是很好奇,那我们接着往下看
Vue进行了如下转化template —> ast —> render函数,最后根据生成的render函数来生成相应的DOM,这里就不拓展讲了。在生成ast和render函数的时候,Vue对v-if这一类指令进行了解析。所以在rend()
进行渲染时,v-if已经开始进行判断了,所以我理解的是,此时insepector中的数据已经被渲染了,所以再次寻找里面的数据的时候就不会报错了。
边栏推荐
- 「PHP基础知识」空值(null)的使用
- C语言 内联函数
- Redis simple study notes
- C语言中关于2的n次方求值问题(移位运算)
- AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/yolov5-5.0/models/commo
- PCL—point cloud data segmentation
- basic operator
- oracle inner join and outer join
- A senior test engineer asked me these questions as soon as the interview came
- @Configuration详解
猜你喜欢

How to check whether a table is locked in mysql

排序学习笔记(二)堆排序

Phospholipid-polyethylene glycol-hydrazide, DSPE-PEG-Hydrazide, DSPE-PEG-HZ, MW: 5000

Problems when yolov5 calls ip camera

UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the index ing argu

MySQL两阶段提交串讲

@Autowired与@Resource区别

MySQL常见的索引

PCL—点云数据分割

分布式领域最重要的一篇论文,到底讲了什么?
随机推荐
利用 nucleo stm32 f767zi 进行USART+DMA+PWM输入模式 CUBE配置
网站与服务器维护怎么做?
语义分割标签即像素值的巨坑,transforms.ToTensor()的错误使用
SSM integration
parser = argparse.ArgumentParser()解析
【我的创作纪念日】 3周年
yolov5调用ip摄像头时出现的问题
MySQL分页查询的5种方法
C语言入门小游戏—三子棋
科研试剂DMPE-PEG-Mal 二肉豆蔻酰磷脂酰乙醇胺-聚乙二醇-马来酰亚胺
APK的安装过程分析 PMS包管理器
【深度学习】从LeNet-5识别手写数字入门深度学习
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
磷脂-聚乙二醇-靶向新生血管靶向肽APRPG,DSPE-PEG-APRPG
啃瓜记录第一天
Deveco studio 鸿蒙app访问网络详细过程(js)
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
【 application 】 life many years of operations, what turned scored 12 k + annual bonus salary?
About cross-domain issues
Problems when yolov5 calls ip camera