vue-cli3使用mock

1/28/2019VueVue CLI 3mock

{% note primary %}
本文介绍了关于Vue CLI 3如何配置mock数据
{% endnote %}

  1. 在根目录新建vue.config.js文件和mock文件夹

  2. 在mock文件夹中存放数据,例如index.json

  3. 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
  1. 如何调用
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
Last Updated:5/25/2024, 2:23:06 AM