vue.config.js

module.exports = {
  devServer: {
      port: 6001.// Port number. If the port number is occupied, it will be automatically increased by 1
      host: "localhost"./ / host name
      https: false./ / agreement
      open: true.// Automatically open browser access when starting the service
      proxy: { // Development environment proxy configuration
          "/dev-api": {// '/dev-api': {
              // Destination server address
              target: "localhost".changeOrigin: true.// Start the proxy server,
              pathRewrite: {
                  '^dev-api' :' '}}}},lintOnSave: false.// Turn off format checking
  productionSourceMap: false.// No.map file is generated during packaging to speed up packaging
}
Copy the code

Install request library

npm install axios --save
Copy the code

Request. Js tools

import axios from 'axios'

const service = axios.create({
  baseURL: "/dev-api".timeout: 10000 // request timeout
})

// Request interceptor
service.interceptors.request.use(
  config= > {
    return config
  },
  error= > {
    return Promise.reject(error)
  }
)

// Response interceptor
service.interceptors.response.use(
  response= > { 
    // Normal response
    const res = response.data
    return res
  },
  error= > {
    // The response is abnormal
    return Promise.reject(error)
  }
)

export default service
Copy the code

API interface

import request from '.. /utils/request'


// Check whether the user name is registered
export function getUserByUsername(username) {
    return request({
        url: `/system/api/user/username/${username}`.method: 'get'})}Copy the code