Configure mock

  • In the development phase, in order to improve efficiency. Need to Mock in advance

    • The form of local loading request static json file (here the follow directory refers to the public directory)
    this.axios.get('/mock/user/login.json').then((res) => {
          
          
        this.res = res
    })
    
    this.axios.get('/user/login').then((res) => {
          
          
        this.res = res
    })
    
    • By installing the plug-in mockjs to use local requests, of course mockjs will not make real requests, which is different from easy-mock and local json
    npm i mockjs --save-dev
    
    • Add code in app.vue
    this.axios.get('/user/login').then((res) => {
          
          
    	this.res = res
    })
    
    • Add the switch of mockjs in main.js and introduce it
    // mock的开关,用于本地测试用,到上线时,关闭
    const mock = true
    if (mock) {
          
          
      require('../public/mock/api.js')
    }
    

    ​ Note: require must be used here, import cannot be used (require is called at runtime, so theoretically require can be used anywhere in the code, import is called at compile time, so it must be placed at the beginning of the file)

Guess you like

Origin blog.csdn.net/qq_39208971/article/details/108295076