1. 安装 npm install mockjs
2. 在src的目录下新建一个文件mock.js
import Mock from 'mockjs' // 引入mockjs
let data =
{
'users|5': [ {
'age|1-100': 100, //100以内随机整数
'birthday': '@date("yyyy-MM-dd")', //日期
'city': '@city(true)', //中国城市
'name': '@cname', //中文名称
}]
}
Mock.mock('data/index', 'get', data) // 根据数据模板生成模拟数据
3. 在main.js中
require('./mock.js');
4. 在页面中使用
import Mock from "mockjs"
import axios from 'axios'
methods: {
// 接口测试
init(){
// 请求后端数据,查询数据源
axios({
method: "get",
url:"data/index",
})
.then((response)=> {
console.log(response)
})
.catch((error)=> {
console.log(error)
})
}
},
mounted () {
this.init()
}