vue-cli4.0安装axios并使用

安装axios包

npm i axios

  • 在main.js同级创建http.js文件夹
  import axios from 'axios'
  
  const http = axios.create({
    baseURL: "http://localhost:3000/admin/api"
  })
  
  export  default http
  • 在main.js中引入http.js
import http from './http.js'
Vue.prototype.$http = http
  • 在vue组件中使用
async fetch(){
	const res = await this.$http.get('categories')
	//const res = await this.$http.post('categories', this.model) //this.model为后端需要的数据,在data中定义
	console.log(res.data)
}

你可能感兴趣的:(vue-cli4.0安装axios并使用)