TypeError: Cannot read property 'split' of undefined

最近跟着慕课网学一个电商项目,学习前端页面出现了如下错误提示:



后面后台管理页面也出现了类似问题:


解决的办法即增加未定义(空字符串)的情况:





以下引用自:https://stackoverflow.com/questions/40340668/vue-js-cannot-read-property-split-of-undefined

ccepted

I even receive the expected result, so why does it throw this error?

Because at some stage, it is undefined. Clearly, if you're ultimately getting the result you expect, it's then becoming defined, but that's later.

You could work around it by doing:

v-for="line in (message.message || "").split('\n')"

...but it would probably be better to look at the greater picture of what that code's doing and find out why message.message is undefined at times it's trying to evaluate that.


As Bill Criswell points out in a comment, you might look at using a computed property on your model for that rather than an in-template expression. E.g.:

var vm = new Vue({
    // ...your other stuff here...

    // Computed properties:
    computed: {
        messageLines: function() {
            return (this.message.message || "").split("\n");
        }
    }
});

then

v-for="line in messageLines"

在你描述的情况中,出现了一个错误信息:TypeError: Cannot read property 'split' of undefined。这个错误表示在代码执行过程中,尝试读取一个未定义的属性并进行字符串分割操作。根据你提供的引用信息,可以看出这个错误发生在mounted hook或onLoad hook中。 这个错误可能有多种原因,其中一种可能是在使用split方法之前,没有正确地初始化或赋值对应的属性。这可能导致代码在执行到split方法时无法读取该属性而抛出错误。 另一个可能的原因是,你在某个地方使用了split方法,但没有提供要分割的字符串。这也会导致类似的错误。 为了解决这个问题,你可以按照以下步骤进行排查: 1. 确保在使用split方法之前,对应的属性已经正确地初始化或赋值。 2. 检查是否存在拼写错误或其他语法错误,特别是在传递参数给split方法时。 3. 如果在组件模式下使用附件功能,确保正确传递数据并进行渲染。 如果以上步骤都没有解决问题,你可能需要进一步排查代码,特别是在涉及到数据处理和传递的部分。你可以查看相关的文档或寻求社区的帮助来解决这个问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Error in mounted hook:TypeError: Cannot read properties of undefined (reading ‘split‘)](https://blog.youkuaiyun.com/m0_61480985/article/details/131664382)[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: 50%"] - *2* [Error in mounted hook:TypeError: Cannot read property ‘XXXXX‘ of undefined](https://blog.youkuaiyun.com/bihaiyanyu/article/details/109216797)[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: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值