vue-cli + mock

惊朝發表於2024-03-31

一定要注意:使用url要用localhost不要用127.0.0.1,這個坑!!!!
還有封裝的axios裡的BASE_URL也要用baseURL: "http://localhost:8080/api"

//vue.config.js
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    setupMiddlewares: (middlewares, devServer)=>{
      if (!devServer) throw new Error("webpack-dev-server is not defined");
      const mockMiddleware = require("./mock/index.js");
      mockMiddleware(devServer.app);
      return middlewares;
    },
  }
})

專案根目錄下/mock/index.js

module.exports = function (app) {
    app.post("/api/user/login", function (req, res) {
      res.json({
        "token": "time 20240331"
      });
    });
};

相關文章