<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-card v-for="item in courses" :key="item" num="2" price="2.00" :desc="item.body" :title="item.id + '-' + item.title" thumb="https://fastly.jsdelivr.net/npm/@vant/assets/ipad.jpeg" @click="ToCardDetail(item)"/> <template slots="loading"> <van-loading class="load-more-loading" /> <span class="load-more-text">加载中...</span> </template> </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.courses = []; this.refreshing = false; } for (let i = 0; i < 10; i++) { this.courses.push(this.list[this.courses.length]); } this.loading = false; if (this.courses.length >= 40) { this.finished = true; } }, 3000); }, onRefresh() { // 清空列表数据 this.finished = false; // 重新加载数据 // 将 loading 设置为 true,表示处于加载状态 this.loading = true; this.onLoad(); }, ToCardDetail(item) { // 获取详情页的路由地址,例如:/detail/1 let detailPath = ../views/CardDetail/${item.id}; // 跳转到详情页 this.$router.push(detailPath); } }, created() { let url = "http://jsonplaceholder.typicode.com/posts" this.axios.get(url).then((res) => { this.list = res.data }) } }; </script> <style scoped> .load-more-loading { margin-right: 6px; } .load-more-text { font-size: 14px; color: #999; } </style>card详情页carddetail的代码怎么写
时间: 2024-01-24 16:17:57 浏览: 166
Auto-Refresh-Chrome-v1.3.16 自定时网页刷新插件
你可以新建一个名为CardDetail的.vue文件,然后在该文件中编写详情页的代码,如下所示:
<template>
<div class="card-detail">
<van-card :title="cardTitle" :thumb="cardThumb">
<div class="card-description">{{ cardDescription }}</div>
</van-card>
</div>
</template>
<script>
export default {
name: "CardDetail",
data() {
return {
cardId: "",
cardTitle: "",
cardThumb: "",
cardDescription: "",
};
},
created() {
this.cardId = this.$route.params.id;
// 根据id获取卡片详情数据
let url = `http://jsonplaceholder.typicode.com/posts/${this.cardId}`;
this.axios.get(url).then((res) => {
this.cardTitle = `${res.data.id}-${res.data.title}`;
this.cardThumb = "https://fastly.jsdelivr.net/npm/@vant/assets/ipad.jpeg";
this.cardDescription = res.data.body;
});
},
};
</script>
<style scoped>
.card-detail {
padding: 20px;
}
.card-description {
margin-top: 20px;
}
</style>
在上述代码中,我们通过 $route.params.id 获取到路由中的卡片id,并根据此id获取卡片的详情数据。最后在页面中展示卡片的标题、缩略图和描述信息。注意,需要在该组件中引入 vant 的 Card 组件。
阅读全文