The introduction of mockjs into the mpvue project will not work.

use mock.js to simulate interface data in mpvue projects

the mock/index.js code is as follows

import config from "@/base/config"
import api from "@/base/net/api"

const Mock = require("mockjs")

const Random = Mock.Random

Mock.mock(config.host + api.GET_MOMENT_LIST, "get", {
  code: 0,
  msg: "",
  "content|20": [
    {
      avatar: Random.image("200x200", "-sharp50B347", "-sharpFFF", ""),
      name: Random.cname(),
      article: Random.cparagraph(60, 120),
      imgs: ["https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1531996965677&di=86dc9bf49eb9866e449873d3dfd2f523&imgtype=0&src=http%3A%2F%2Fimg4.duitang.com%2Fuploads%2Fitem%2F201408%2F12%2F20140812102945_anHrL.jpeg", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1532023095731&di=4b189ec02e76303b074c7415df141e03&imgtype=0&src=http%3A%2F%2Fpic.yesky.com%2FuploadImages%2F2016%2F342%2F49%2F5DY56992PG2D.jpg", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1532023130033&di=8215d336148a1c45d77693b4360354b2&imgtype=jpg&src=http%3A%2F%2Fimg0.imgtn.bdimg.com%2Fit%2Fu%3D1649847555%2C3026466189%26fm%3D214%26gp%3D0.jpg"],
      addr: Random.city(),
      time: Random.integer(1, 59) + "",
      zan: Random.integer(0, 100),
      comments: []
    }
  ]
})

then add

to the main.js
require("@/mock/index")

attach
api.js

export default {
  GET_MOMENT_LIST: "/moments"
}

config.js

export default {
  host: "http://cun.me" // 
}

but it is still 404 at the time of request, so what is missing

question add:

/ mock/index.js

import Mock from "mockjs"
import config from "@/base/config"
import api from "@/base/net/api"

console.log(Mock)
console.log(Mock.setup)

Mock.setup({
  timeout: "300-1200"
})

this setup is a method, how can it be reported as property, and it is still of undefined

Mar.28,2021

404 does not find the corresponding url, or whether there is something wrong with the configuration in index.js. Try to see if you can get through with an easy version, and you don't even need to specify get or post.

related references:
Mock failed with 404 Error
mock.js- does not need to wait, Let the front end develop independently of the back end
explain the use of mockjs (request for data deletion) in vue-cli projects


ask the same question. There is no problem with the configuration, but a Mock.setup ({timeout:400}) has been added, and it has been reported that setup is not defined

.
Menu