use mockjs in nuxt project

来源:www.jianshu.com 更新时间:2023-05-25 21:55

1.change nuxt.config.js file,add an subitem under plugin

{ src: '~plugins/mockjs', ssr: true }

2.create mockjs.js under plugin folder. the content is:


var Mock = require('mockjs')

Mock.mock(/user\.json/, {
  'list|1-10': [{
    'id|+1': 1,
    'email': '@EMAIL'
  }]
})
  1. send a request as normal in vue pages
import axios from 'axios'
...
export default {
    methods: {
    getMockUser () {
      axios.get('user.json')
        .then((res) => {
          console.log('GET MOCK USER====', res.data)
        })
    },
    ...
}