+
80
-

vue3如何引入和使用axios?

vue

vue3如何引入和使用axios?


网友回复

+
0
-

1、安装axios

npm install axios --save-dev

2、main.js中全局引入

import axios from 'axios'
const app = createApp(App)
app.config.globalProperties.$http = axios

3、页面引用,通过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)
    })

post

proxy.$http.post('/a', {
        "id": 5,
        "name": "bfw"
    }).then(response => {
        console.log(response.data)
    }, error => {
        console.log('错误', error.message)
    })

我知道答案,我要回答