安装与配置axios

cd bookstore
npm install axios vue-axios -S

在main.js中:

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import VueAxios from 'vue-axios'

createApp(App).use(store).use(router).use(VueAxios,axios).mount('#app')

在项目根目录下的vue.config.js中的原始代码:

const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true
})

需要更改为:

module.exports = {
  devServer: {
    proxy:{
      ///api是后端数据接口的上下文路径
      '/api':{
        //这里的地址是后端数据接口的地址
        target:'http://111.229.37.167/',
        //允许跨域
        changeOrigin:true,
      }
    }
  }
}

接下来在main.js中为axios配置全局的baseURL默认值:

axios.defaults.baseURL = '/api'

你可能感兴趣的:(VUE,vue.js,javascript,前端)