{% note primary %}
本文介绍了关于Vue CLI 3如何配置mock数据
{% endnote %}
在根目录新建
vue.config.js
文件和mock
文件夹在mock文件夹中存放数据,例如
index.json
vue.config.js
进行配置
const mockIndex = require('./mock/index.json');
module.exports = {
devServer: {
port: 8080,
before(app) {
app.get('/api/index', (req, res) => {
res.json(mockIndex);
});
}
}
};
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
- 如何调用
import axios from 'axios';
export default {
name: 'home',
data() {
return {
info: null
}
},
mounted() {
axios
.get('/api/index')
.then(res => { this.info = res })
},
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15