mock数组数据:
import Mock from ‘mockjs’
// 获取用户的 //响应模板
Mock.mock(/api\/sendwork/, {
'code': 2000,
'data|2-10': [
{
'SNO|+1': 1,
'name': '@CNAME',
'ischecked': false,
'account|+1': 10000,
'SNO|+1': 1,
'avatar': {
'url': '@IMAGE(50x50)'
},
}
]
})
//获取历史发送人列表
Mock.mock(/api\/sendhistory/, {
'code': 2000,
'data|4-10': [
{
'SNO|+1': 1,
'name': '@CNAME',
'ischecked': false,
'account|+1': 10000,
'SNO|+1': 1,
'avatar': {
'url': '@IMAGE(50x50)'
}
}
]
})
//历史联系人默认值
const initUserHistory = Mock.mock([
{
'SNO|+1': 1,
'name': '@CNAME',
'ischecked': false,
'account|+1': 10000,
'SNO|+1': 1,
'avatar': {
'url': '@IMAGE(50x50)'
}
}
])
//调用接口 async await 表示先响应请求(否则接口返回的是promise)
const [records, setRecords] = useState([])
// 获取用于ui渲染的数据
useEffect(() => {
const fetchRecords = async () => {
try {
const res = await request.get('/api/sendwork/')
//console.log('请求结果', res)
setRecords(res)
} catch (error) {
console.log(error)
}
}
fetchRecords()
}, [])
2.循环获取数组中id对应的url
// mock
Mock.mock(/announcement\/result/, {
code: 2000,
'data|3-4': [{
'id|+1': 1,
'url': '/introduceLession',
'image': {
"url": require("./images/announcement.png"),
}
}]
})
// 数组
const noticeList = [
{
iconSrc: require("./images/announcement.png"),
to: '/free-lesson',
}, {
iconSrc: require("./images/announcement.png"),
to: '/question-bank',
}, {
iconSrc: require("./images/announcement.png"),
to: '/work-station',
}, {
iconSrc: require("./images/announcement.png"),
to: '/person-center',
}
]
<Carousel
autoplay
className={nList ? "autoplay-img":"empty-img"}
>
{
nList.map((item, index) =>
<div key={item.id} onClick={() => window.open(item.url)}>
<h3><img src={item.image.url} alt="" /></h3>
</div>
)
}
</Carousel>