Property or method “approval1“ is not defined on the instance but referenced during render

出现这个错误的原因是我们的方法需要放在了最后,只要将定义的方法放在前面即可

 

这个错误表明在 `fetchApprovalList` 方法中,`newData` 变量未定义。这是因为在拼接分页数据时,代码中直接使用了 `newData`,但该变量未在正确的作用域中声明。以下是修复后的代码,并优化了分页逻辑: ### 修复后的代码 ```html <script> import { approval_getList } from '@/api/approve.js'; export default { data() { return { approvalList: [], loading: false, pageNum: 1, pageSize: 10, total: 0, noMoreData: false }; }, methods: { fetchApprovalList() { this.loading = true; const approvalData = { pageNum: this.pageNum, pageSize: this.pageSize, approval: { remark: '', id: 0, approvalType: 0, startTime: '', endTime: '' } }; return approval_getList(approvalData).then(response => { this.total = response.total || 0; const newData = response.rows || []; // 正确声明 newData // 分页数据拼接 if (this.pageNum === 1) { this.approvalList = newData; } else { this.approvalList = [...this.approvalList, ...newData]; } // 检查是否已加载全部数据 this.noMoreData = this.approvalList.length >= this.total; }).catch(error => { console.error('获取审批列表失败:', error); uni.showToast({ title: '加载失败', icon: 'none' }); // 失败时回退页码(仅对加载更多操作) if (this.pageNum > 1) { this.pageNum--; } }).finally(() => { this.loading = false; }); } } }; </script> ``` ### 关键修复点 1. **正确声明 `newData`** 在 `approval_getList(approvalData).then()` 回调中,使用 `const newData = response.rows || [];` 显式声明变量,避免作用域问题。 2. **优化分页拼接逻辑** - 第一页数据直接覆盖 `this.approvalList`。 - 后续页数据通过扩展运算符拼接:`[...this.approvalList, ...newData]`。 3. **错误处理增强** - 加载失败时,如果是加载更多操作(`pageNum > 1`),回退页码以避免页码错乱。 4. **移除冗余代码** 删除了重复的 `noMoreData` 判断逻辑,直接通过 `this.approvalList.length >= this.total` 更新状态。 ### 完整优化后的代码 ```html <template> <view class="container"> <view class="list-item" v-for="item in approvalList" :key="item.id" @click="toDetail(item)"> <!-- 原有列表内容 --> </view> <view v-if="!loading && approvalList.length === 0" class="empty">暂无审批数据</view> <view v-if="loading && approvalList.length === 0" class="loading">加载中...</view> <view v-if="loading && approvalList.length > 0" class="loading-more">加载更多...</view> <view v-if="!loading && noMoreData" class="no-more">没有更多数据了</view> </view> </template> <script> import { approval_getList } from '@/api/approve.js'; export default { data() { return { approvalList: [], loading: false, pageNum: 1, pageSize: 10, total: 0, noMoreData: false }; }, onShow() { this.initData(); this.fetchApprovalList(); }, onPullDownRefresh() { this.initData(); this.fetchApprovalList().finally(() => uni.stopPullDownRefresh()); }, onReachBottom() { if (this.loading || this.noMoreData) return; this.pageNum++; this.fetchApprovalList(); }, methods: { initData() { this.pageNum = 1; this.approvalList = []; this.noMoreData = false; }, fetchApprovalList() { this.loading = true; const approvalData = { pageNum: this.pageNum, pageSize: this.pageSize, approval: { remark: '', id: 0, approvalType: 0, startTime: '', endTime: '' } }; return approval_getList(approvalData) .then(response => { this.total = response.total || 0; const newData = response.rows || []; this.approvalList = this.pageNum === 1 ? newData : [...this.approvalList, ...newData]; this.noMoreData = this.approvalList.length >= this.total; }) .catch(error => { console.error('获取审批列表失败:', error); uni.showToast({ title: '加载失败', icon: 'none' }); if (this.pageNum > 1) this.pageNum--; // 加载更多失败时回退页码 }) .finally(() => this.loading = false); }, toDetail(item) { uni.navigateTo({ url: `/pages/work/detail?item=${encodeURIComponent(JSON.stringify(item))}` }); } } }; </script> <style> /* 原有样式 */ .loading-more, .no-more { text-align: center; padding: 20rpx; color: #999; font-size: 14px; } </style> ``` ### 测试建议 1. **下拉刷新**:检查是否能正确重置数据并重新加载第一页。 2. **上滑加载更多**:验证分页数据是否正确拼接,且不会重复请求。 3. **错误处理**:模拟网络错误,确认页码回退和用户提示是否正常。 4. **边界情况**:测试空数据、单页数据不足 `pageSize` 等场景。 通过以上修复,分页逻辑和错误处理更加健壮,避免了变量未定义的错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值