Uncaught (in promise) TypeError: Cannot read properties of null (reading ‘xxx’)
报错:
Uncaught (in promise) TypeError: Cannot read properties of null (reading ‘xxx’)
解决方案:
使用ES2020 引入了可选链操作符(Optional Chaining Operator)。a.b.c
改为 a?.b?.c
写法。
<!-- ❌错误代码: -->
<el-table><template #default="scope"><!-- 这种连环取值,大概率发生问题:row.data.type,其中有一个值为空就完了 -->{{ scope.row.data.type }}</template>
</el-table><!-- ✔正确代码: -->
<el-table><template #default="scope"><!-- 问号,代表可为空,不会引发连环取值报错 -->{{ scope.row?.data?.type }}</template>
</el-table>