cannot read properties of undefined(reading ‘kingKongList‘)问题处理方法

文章讲述了在Vue项目中,由于模板渲染速度比数据请求快,可能导致首次渲染时数据为空引发错误。解决方法是使用v-if对数据存在情况进行判断,避免在数据未加载时遍历。同时提到在小程序开发工具中的不同行为和一种可能的临时解决方案:禁用不必要的浏览器扩展。
摘要由CSDN通过智能技术生成

产生这个错误的原因是:vue模板渲染解析要比发请求要快,所以界面可以正常显示,但是还是会报错,在你第一次渲染时kingKongList列表就是空的。

但在小程序开发工具中是不报错的,原因是小程序编译之后的代码放到开发工具里加载,所以不会出现以上情况

解决方法:添加条件,有值的时候再渲染

v-if:添加删除

v-show:显示、隐藏DOM display:none

//index.vue
<template>
	<view class="indexContainer">
		<!--头部-->
		<view class="header">
			<image class='logo' src="/static/images/logo.jpg" mode=""></image>
			<view class="search">
				<text class='iconfont icon-sousuotianchong'></text>
				<input type="text" value=""placeholder='搜索...'placeholder-class='placeholder'/>
			</view>
			<button type="default">张三张三</button>
		</view>
		<!--滑块-->
		<scroll-view class='navscroll' scroll-x='true' >
			<view class="navItem " :class="{active:navIndex==-1}" @click="changeIndex(-1)" >推荐</view>
			<view class='navItem ':class="{active:navIndex==index}"@click="changeIndex(index)" v-if="indexData" v-for="(item,index) in indexData.kingKongModule.kingKongList":key="item.L1Id">{{item.text}}</view>
			
		</scroll-view>
		
	</view>
</template>

<script>
	import request from '../../utils/request.js'
	export default {
		data() {
			return {
				indexData:{},  //首页数据
				navIndex:-1,//点谁,谁高亮,导航的标记
			}
		},
		onLoad() {

		},
		created(){
			this.getIndexData();
		},
		methods: {
			//获取首页数据
			async getIndexData(){
				//const res=await request('/getIndexData');//小程序
				const res= await request('/api/getIndexData');//H5
				console.log('========',res);
				this.indexData=res.indexData;
			},
			//点击切换下标
			changeIndex(index){
				this.navIndex=index;
			}
		}
	};
</script>

<style lang='stylus'>
	.indexContainer
		.header
			display flex
			padding 10rpx 5rpx
			.logo 
				width 140rpx
				height 50rpx
			.search
				width 420rpx
				height 50rpx
				background #f5f5f5
				position relative
				input
					width 360rpx
					height 60rpx
					margin-left 60rpx
					.placeholder
						font-size 26rpx
						color #333
				.iconfont
					position absolute
					font-size 40rpx
					left 4rpx
					top 4rpx
			button
				width 144rpx
				height 50rpx
				line-height 60rpx
				text-align center
				font-size 27rpx
				padding 0 4rpx
		.navscroll
		//不换行
			white-space:nowrap
			.navItem
				display:inline-block
				height:40rpx
				padding:10rpx
				font-size:26rpx
				&.active
					color:#BB2C08
					border-bottom 3rpx solid #BB2C08
	
	
	.test
		color:red
</style>

这时候还是会出现问题,因为v-for比v-if的优先级要更高,还没有判断是否值为空就已经开始遍历了,所以v-if和v-for不能放在同一个标签上,应该把v-if提到父标签上去

老是出现以下错误

最好的解决办法是把所运行的浏览器的没必要的扩展插件该删的删就可以了

这个错误"Cannot read properties of undefined (reading 'matched')"通常是由于访问了一个未定义的属性而引起的。根据引用,这个错误可能是在访问一个未定义的'matched'属性时出现的。 根据引用,解决这个问题方法是检查导入的router实例对象的属性名是否正确。确保在实例化对象中的属性名和值名一致。 另外,根据引用,一种解决方法是将导入的Router改成小写的router,并且将所有相关的Router都改成小写。 综上所述,如果你遇到了"cannot read properties of undefined reading matched"错误,你可以检查一下导入的对象属性名是否正确,并将相关的属性名改成小写,以解决这个问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [TypeError: Cannot read properties of undefined (reading ‘matched‘)“错误](https://blog.csdn.net/xuehua_zhx/article/details/122112017)[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* [解决问题:Cannot read properties of undefined (reading ‘matched‘)“报错](https://blog.csdn.net/qq_55761697/article/details/125402362)[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 ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值