Vue使用Mook模拟数据

1.在项目根目录建立 vue.config.js

module.exports = {
  devServer: {
    before(app, server) {
      // get请求
      app.get("/api/cartList", (req, res) => {
        res.json({
          result: [
            {
              id: 0,
              title: "烤鸡翅",
              price: 15,
              active: true,
              count: 1
            },
            {
              id: 1,
              title: "烤腰子",
              price: 28,
              active: true,
              count: 1
            },
            {
              id: 2,
              title: "烤大蒜",
              price: 50,
              active: true,
              count: 1
            },
            {
              id: 3,
              title: "烤鸡脖",
              price: 88,
              active: true,
              count: 1
            }
          ]
        });
      });
    }
  }
};

随后重启项目 模拟出来的后端接口在你项目的地址/api/cartList

下载axiosmain.js全局挂载

import axios from "axios";
Vue.prototype.$http = axios;

最后在任意一个组件内使用 this.$http 进行axios请求

Leave a Reply