use mockjs in nuxt project

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)
        })
    },
    ...
}    

你可能感兴趣的:(use mockjs in nuxt project)