介绍
- 网上很多中这种方法,但是不知道为什么我使用
app.config.globalProperties.$http=
没办法使用,不知道啥情况,所以这里只记录可以使用的app.provide('$http',axios)
。
- 基本来说,对于前端来说,跟后端的数据交互是一件很常见也是很重要的事,因为我才刚接触vue,所以对于这一块不是很了解,今天介绍最普遍的两种方式。
直接绑定
async function getToken1(){
await axios.post('url',userInfo)
.then(response=>{
console.log(response.data)
})
.catch(error=>{
console.log(error)
})
}
const userInfo={
email:'aaa.com',
password:'bbb'
}
全局设置
import { createApp } from 'vue'
import App from './App.vue'
import axios from "axios";
const app=createApp(App)
axios.defaults.baseURL='http://pengqian.work:17428/api/v1'
app.provide('$http',axios)
app.mount('#app')
const $http=inject('$http')
async function getToken(){
await $http.post('/tokens',userInfo)
.then(response=>
{
console.log(response.data)
})
.catch(error=>
{
console.log(error)
})
}