1、安装axios
npm install axios --save-dev
2、main.js中全局引入
import axios from 'axios' const app = createApp(App) app.config.globalProperties.$http = axios3、页面引用,通过formdata方式请求
import { getCurrentInstance } from 'vue';
const { proxy } = getCurrentInstance()
proxy.$http({
method: 'post',
url: 'url地址',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: JSON.stringify({words:word})
}).then((response)=>{
console.log(response)
})通过json请求get
proxy.$http.get('/a?id=1').then(response => {
console.log( response.data)
})postproxy.$http.post('/a', {
"id": 5,
"name": "bfw"
}).then(response => {
console.log(response.data)
}, error => {
console.log('错误', error.message)
})
网友回复


