: Cannot read properties of undefined (reading 'split')"
时间: 2023-10-15 22:26:04 浏览: 74
"Cannot read properties of undefined (reading 'split')"是一个报错信息,表明在调用split()方法时,属性为undefined,无法执行split()方法。这个报错通常出现在对数据进行分割之前没有对数据进行非空判断的情况下。因此,在调用split()方法之前,需要先对需要分割的数据进行非空判断,以避免出现异常。比如,在处理图片链接的时候,可以先判断链接是否存在,然后再进行分割操作,如下所示:
```
const imgUrl = "https://tu.cloud.cn/flower/flower-size.jpg?size=1800";
if (imgUrl) {
const newImgUrl = imgUrl.split('?')[0 + "?size=600";
console.log(newImgUrl); // 输出:https://tu.cloud.cn/flower/flower-size.jpg?size=600
}
```
这样,当imgUrl不为空时,才会执行分割操作,避免出现报错信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [【vue】前端报错:Cannot read properties of undefined (reading ‘split‘)](https://blog.csdn.net/Triumph_light/article/details/123937699)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [【报错】 “TypeError: Cannot read properties of undefined (reading ‘split‘)](https://blog.csdn.net/qq_53810245/article/details/125601613)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [几个常用的JavaScript字符串处理函数 – split()、join()、substring()和indexOf()](https://download.csdn.net/download/weixin_38747087/13666208)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]