fake/mock.js
import axios from "@/axios.js"
const MockAdapter = require('axios-mock-adapter')
const mock = new MockAdapter(axios) // This sets the mock adapter on the default instance
export default mock
fake/index.js
import mock from './mock'
mock.onGet('/api/files').reply(() => {
const response = {
Status: true,
Message: '',
Data: null
}
return [200, response]
})
接著在 main.js
中引入。之後若要移除仿造資料,只要註解此行即可。
main.js
import './fake/index.js'
補充:onGet 與呼叫 API 的路徑要完全一致,如下:
axios.get('/progress?id=1&token=abcde')
mock.onGet('/progress?id=1&token=abcde')
// or use Regex
mock.onGet(/\/progress\/?.*/)
建議將參數額外傳送:
axios.get('/progress', {
params: {
id: 1,
token: abcde
}
})
// mock 可以不用正規表示式
mock.onGet('/progress')