一、npm 安装axios,文件根目录下安装,指令如下
npm install axios
二、修改原型链,在main.js中引入axios
import axios from 'axios'
接着将axios改写为Vue的原型属性,
Vue.prototype.$http=axios
这样之后就可在每个组件的methods中调用$http命令完成数据请求
三、在组件中使用
methods: {
get(){
this.$http({
method:'get',
url:'/url',
data:{}
}).then(function(res){
console.log(res)
}).catch(function(err){
console.log(err)
})
this.$http.get('/url').then(function(res){
console.log(res)
}).catch(function(err){
console.log(err)
})
}
}
有关axios的配置请参考如下文档,点击打开链接
本文介绍了如何使用npm安装Axios库,并将其集成到Vue项目中。通过修改原型链,使得Axios可以在各个组件中作为Vue原型属性直接调用。此外,还提供了具体的组件内请求示例。
705

被折叠的 条评论
为什么被折叠?



