axios安装,配置及配合vue脚手架

2022年10月21日12:16:21

1.安装axios

npm install axios

2.脚手架中使用axios

main.js中导入axios
import axios from'axios';

//在main.js中可全局配置默认
axios.defaults.baseURL ='https://api.example.com'; //基础URL
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;  //作者
axios.defaults.headers.post['Content-Type'] ='application/x-www-form-urlencoded'; //默认请求头

3.组件中使用axios

<script>
import axios from'axios';;//引入axios实例

// 配置发起一个post请求
//axios 的post请求要注意有的是通过req.body发送数据,有的是通过req.params发送数据
axios({method: 'post',
  url: '/user/12345',
  //axios 的post请求需要设置请求头
  headers:{'Content-Type':'application/json',},
  data:{
  //data 中的数据要根据接口请求数据类型看是否使用JSON.stringify()方法转成字符串firstName:'Fred',lastName:'Flintstone'}// data: JSON.stringify({
   //phone: this.user,
  //pwd: this.psd,
 //}),});
// 在 node.js 用GET请求获取远程图片
axios({method:'get',url:'http://bit.ly/2mTM3nY',responseType:'stream'})
  .then(function (response){
    response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))});

</script>

axios-http

  • 作者:werch
  • 原文链接:https://blog.csdn.net/weixin_42666092/article/details/118056123
    更新时间:2022年10月21日12:16:21 ,共 884 字。