<template> <van-pull-refresh v-model="refreshing" @refresh="onRefresh"> <van-list v-model:loading="loading" :finished="finished" finished-text="没有更多了" @load="onLoad"> <!-- <van-cell v-for="item in courses" :key="item" :title="item.title" /> --> </van-list> </van-pull-refresh> </template> <script> export default { name: "LoadMore", data() { return { list: [], courses: [], loading: false, finished: false, refreshing: false } }, methods: { onLoad() { setTimeout(() => { if (this.refreshing) { this.list=[]; this.refreshing = false; } for (let i = 0; i < 10; i++) { this.list.push(this.list.length+1); } this.loading= false; if (this.list.length >= 40) { this.finished = true; } }, 3000); }, onRefresh() { // 清空列表数据 this.finished = false; // 重新加载数据 // 将 loading 设置为 true,表示处于加载状态 this.loading = true; this.onLoad(); } }, /* created() { let url = "http://jsonplaceholder.typicode.com/posts" this.axios.get(url).then((res) => { this.list = res.data }) } */ }帮我检查代码出现的错
时间: 2023-12-26 21:04:32 浏览: 78
react-native-smooth-pull-to-refresh:自定义拉动以刷新React Native的组件
在这段代码中,你将 `courses` 数组作为模板中 `van-cell` 的数据源,但是在 `onLoad` 方法中,你却将数据添加到了 `list` 数组中,并没有更新 `courses` 数组。因此,你需要将 `onLoad` 方法中的数据添加到 `courses` 数组中,以便模板能够正确地显示数据。
另外,在 `onRefresh` 方法中,你没有清空 `list` 数组,这也会导致出现数据重复的问题。你需要在刷新数据之前清空 `list` 数组,以确保数据能够正确地刷新。
下面是更新后的代码:
```
<template>
<van-pull-refresh v-model="refreshing" @refresh="onRefresh">
<van-list v-model:loading="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
<van-cell v-for="item in courses" :key="item" :title="item.title" />
</van-list>
</van-pull-refresh>
</template>
<script>
export default {
name: "LoadMore",
data() {
return {
courses: [],
loading: false,
finished: false,
refreshing: false
};
},
methods: {
onLoad() {
setTimeout(() => {
if (this.refreshing) {
this.courses = [];
this.refreshing = false;
}
for (let i = 0; i < 10; i++) {
this.courses.push({ title: "课程" + (this.courses.length + 1) });
}
this.loading = false;
if (this.courses.length >= 40) {
this.finished = true;
}
}, 3000);
},
onRefresh() {
// 清空列表数据
this.finished = false;
this.courses = [];
// 重新加载数据
// 将 loading 设置为 true,表示处于加载状态
this.loading = true;
this.onLoad();
}
}
};
</script>
```
阅读全文