Vue问题篇:报错 : Error in render: “TypeError: Cannot read property ‘xxx‘ of undefined

问题描述:

页面渲染成功,但在控制台中却出现类似如下的报错:

原因:

一般出现这种报错,原因就是在接口数据未获取之前,使用了深层属性,即多层的 . ,如定义好data:

data() {
    return {
        chartData: {
            top: {},
            bottom: {},
        },
   }
}

但在模板中却使用了 top.columns[0] 这种深层属性,这时top为空,top.columns为undefined,top.columns[0]自然会报错。

解决办法:

解决这种问题的方法也比较简单,就是在使用深层属性的html节点上使用v-if,如:

<el-radio-group v-model="formOption.period" @change="initAllChart">
    <el-radio-button :label="topPeriods[0]['id']" v-if="topPeriods[0]['id']"
                     style="position: absolute;top: 10px;left: 200px;">
        {{ topPeriods[0]['name'] }}
    </el-radio-button>
</el-radio-group>

这样,在未获取接口数据之前,vue实例不会渲染该节点,也就不会报错了。

如果在template中使用v-for,则需要将变量定义在data中,不可定义在computed中,因为在data中可以定义数据类型,computed则不能定义,如下,定义topPeriod是为数组,则在v-for中自动识别,空数组不渲染。

data() {
    return {
        topPeriods : []
    }
},
<template v-for="(topPeriod, index)  in topPeriods">
    <el-radio-button :label="topPeriod['id']" v-if="topPeriod"
                     :style="buttonStyle(index)">
        {{ topPeriod['name'] }}
    </el-radio-button>
</template>

总结:

1  在使用未定义属性的节点上使用v-if。

2  在data中定义变量类型。

注意点:

这里有几个需要注意的点,也是我踩过的坑:

1  在上述例子中,如果父节点上使用 v-if="topPeriods[0]" 或 v-if="topPeriods" , Vue 仍会报错,我在查询到的一些博客中提出这也是解决问题的一种方法,但其实并不能解决问题。

2 也有一些博客提出,将mounted()修改为created(),也就是在Vue生命周期中的created阶段去获取所有数据,然后更新dom时就不会报错了,这种方法也不能解决问题,因为一般异步调用在mounted()执行后并未完成,他就继续更新数据,并在update()阶段实时更新dom,同样会报错,测试如下图:

我在vue生命周期中打印提示,可以看出,无论是created()还是mounted()执行完之后,仍会执行接口调用数据,从而引起错误。

beforeCreate() {
    console.log("beforecreated")
},

beforeMount() {
    console.log("beforeMounted")
},
mounted() {
    console.log("mounted");
},
created() {
    console.log('createdBegin');
    this.getRegions();
    this.getYears();
    this.getTypes();
    this.getPeriods();

    this.initFullScreenChart();
    console.log('createdEnd');
},
beforeDestroy() {
    console.log("beforeDestroy")
},
destroyed() {
    console.log("destoryed");
},
beforeUpdate() {
    console.log("beforeUpdate");
},
updated() {
    console.log("update");
},
watch : {
    'formData.years' : function (newVal) {
        console.log('years 更新了')
    },
    'formData.regions' : function (newVal) {
        console.log('regions 更新了')
    },
    'formData.types' : function (newVal) {
        console.log('types 更新了')
    },
    'formData.periods' : function (newVal) {
        console.log('periods 更新了')
    },
    'chartData.top' : function ( ) {
        console.log('top更新了');
    },
    'chartData.bottom' : function () {
        console.log('bottom更新了');
    }
}

 

  • 12
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误是由于在Vue渲染过程中,尝试读取一个未定义或为空的属性'slice'引起的。具体来说,错误提示了无法读取undefined对象的'slice'属性。这个错误常见于使用v-for指令时,对一个未定义或为空的数组进行循环迭代。 根据提供的代码片段,问题可能出现在"typeIdPro.sysqalist"这个数组上。在代码中,使用了v-for指令来遍历这个数组,并对其进行slice操作。但是如果"typeIdPro.sysqalist"是未定义或为空,就无法对其进行slice操作,从而引发了这个错误。 解决这个问题的方法是,在渲染代码之前,确保"typeIdPro.sysqalist"已经被正确地定义和赋值。你可以检查数据的接口是否成功请求到了数据,如果数据请求失败或返回一个空数组,那么就需要相应地处理这种情况,比如在数据请求失败时显示一个错误提示,或者在数据为空时显示一个占位符。 另外,为了避免类似的错误,你还可以在使用v-for指令之前,使用v-if指令来判断数组是否已经定义或为空,只有在数组存在且不为空的情况下才进行循环迭代操作。这样可以防止出现类似的错误提示。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [解决[Vue warn]: Error in render: “TypeError: Cannot read properties of undefined (reading ‘*******...](https://blog.csdn.net/LanceYAZ/article/details/123957730)[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_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [[Vue warn]: Error in render: "TypeError: Cannot read property 'slice' of undefined" found in](https://blog.csdn.net/dianwan5205/article/details/102082506)[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_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值