在做前端开发的时候,少不了要用一些模拟的json的数据来进行测试,在没有拿到后端开发小伙伴的接口之前,就先尝试着自己写一下json数据吧,前面有说过,使用mock的方法来实现,有小伙伴可能不太习惯,那么json存放的文件夹以及在组件里面要用到的路径一定要注意哦。
比如:我首先要在static的静态资源文件夹底下新建一个json的数据,在每个组件页面,也就是pages底下的组件里面使用的时候,要注意一下url的请求路径,如果路径写错了会报404的错误。
<template>
<view class="page">
<view class="uni-list">
<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="item in itemList">
<view class="uni-list-cell-navigate uni-navigate-right uni-media-list ">
<view class="uni-media-list-logo">
<image v-if="showImg" :src="item.thumbnail_pic_s"></image>
</view>
<view class="uni-media-list-body">
<view class="uni-media-list-text-top">{{item.author_name}}</view>
<view class="uni-media-list-text-bottom uni-ellipsis">{{item.title}}</view>
</view>
</view>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
showImg: false,
itemList: []
}
},
onLoad() {
this.getList();
this.showImg = true;
},
methods: {
getList() {
uni.request({
url: '../../static/1.json',
success: (res) => {
console.log(res.data);
this.itemList = res.data.result.data;
}
});
}
}
}
</script>
<style>
.title {
padding: 20upx;
}
.uni-navigate-right.uni-media-list {
height: 80px;
}
</style>
json模拟的数据
{
"reason": "成功的返回",
"result": {
"stat": "1",
"data": [{
"uniquekey": "f0814cd34eb5e68c77e236a1f384c6d8",
"title": "梅奥在辽宁男篮的作用会很大?看完分析,让球迷哑口无言",
"date": "2020-06-09 09:17",
"category": "头条",
"author_name": "我叫五球王",
"url": "https:\/\/v.juhe.cn\/toutiao\/s?id=https%3A%2F%2Fmini.eastday.com%2Fmobile%2F200609091709351.html",
"thumbnail_pic_s": "https:\/\/09imgmini.eastday.com\/mobile\/20200609\/2020060909_262cb8c58ea1492fa7b92ebf273636ec_0093_mwpm_03200403.jpg",
"thumbnail_pic_s02": "http:\/\/09imgmini.eastday.com\/mobile\/20200609\/2020060909_234fe6c9318147a19ead98e025966324_6371_mwpm_03200403.jpg",
"thumbnail_pic_s03": "http:\/\/09imgmini.eastday.com\/mobile\/20200609\/2020060909_1615cd0d25494899b61bf7565be59ecd_4542_mwpm_03200403.jpg"
}, {
"uniquekey": "e39022a8c029f082c7a96690458a3f6e",
"title": "李霄鹏带来一个好消息!鲁能再次受益,球队或将迎来重大转折",
"date": "2020-06-09 07:35",
"category": "头条",
"author_name": "我名叫大空翼",
"url": "https:\/\/v.juhe.cn\/toutiao\/s?id=https%3A%2F%2Fmini.eastday.com%2Fmobile%2F200609073533157.html",
"thumbnail_pic_s": "https:\/\/08imgmini.eastday.com\/mobile\/20200609\/2020060907_783dc529f66b4b31b8908a8f8375acd0_5893_mwpm_03200403.jpg",
"thumbnail_pic_s02": "http:\/\/08imgmini.eastday.com\/mobile\/20200609\/2020060907_c602653f4f63457795940a17a27dbdab_0220_mwpm_03200403.jpg",
"thumbnail_pic_s03": "http:\/\/08imgmini.eastday.com\/mobile\/20200609\/2020060907_2162745719d24b53ab460a539a65f7a3_7076_mwpm_03200403.jpg"
}, {
"uniquekey": "7ce47100913dd0a3c3c90630f5814900",
"title": "足坛富豪榜:英超无名之辈登顶 财富是C罗的44倍",
"date": "2020-06-09 07:35",
"category": "头条",
"author_name": "网易体育",
"url": "https:\/\/v.juhe.cn\/toutiao\/s?id=https%3A%2F%2Fmini.eastday.com%2Fmobile%2F200609073526006.html",
"thumbnail_pic_s": "https:\/\/03imgmini.eastday.com\/mobile\/20200609\/20200609073526_6205570aeee2e1cd9225f35d7cfb4245_1_mwpm_03200403.jpg"
}, {
"uniquekey": "e4dd86380f130907a649a26b8533dbd1",
"title": "中国乒乓球界的五位大佬,张继科只排名第二,他才是第一",
"date": "2020-06-09 07:32",
"category": "头条",
"author_name": "远方全民体育",
"url": "https:\/\/v.juhe.cn\/toutiao\/s?id=https%3A%2F%2Fmini.eastday.com%2Fmobile%2F200609073213662.html",
"thumbnail_pic_s": "https:\/\/07imgmini.eastday.com\/mobile\/20200609\/20200609073213_96f6864ab45b54367a9e52c059106ce4_6_mwpm_03200403.jpg",
"thumbnail_pic_s02": "http:\/\/07imgmini.eastday.com\/mobile\/20200609\/20200609073213_96f6864ab45b54367a9e52c059106ce4_2_mwpm_03200403.jpg",
"thumbnail_pic_s03": "http:\/\/07imgmini.eastday.com\/mobile\/20200609\/20200609073213_96f6864ab45b54367a9e52c059106ce4_3_mwpm_03200403.jpg"
}]
},
"error_code": 0
}
一个简单的列表页如下: