- 首页 > it技术 > >
src
目录下新建index.js
和response
目录,response
用于注册各个模块的接口
- 编写
index.js
import Mock from 'mockjs'let moduleList = require.context('./response', true, /.js$/)
moduleList.keys().forEach(key => {
let el = moduleList(key)
for (const i in el) {
if (Object.hasOwnProperty.call(el, i)) el[i]()
}
})Mock.setup({
timeout: '0-100' // 也支持具体数字
})export default Mock
【vue中mockjs的使用】3.编写示例 response
目录下新建user.js
,内容如下
import Mock from 'mockjs'const Random = Mock.Random
export const getUserList = () => {
Mock.mock(/user\/getUserList/, 'get', {
'data|1-10': [
{
'id|+1':0
email: '@email',
name: '@cname',
address: '@county(true)'
}
]
})
}
4.使用
// 直接调用接口
import { getUserList } from '@/api/user'{
methods: {
getUserList(){
getUserList().then(res => console.log(res))
}
}
}
推荐阅读