Rumah  >  Artikel  >  hujung hadapan web  >  vuejs中axios用法是什么

vuejs中axios用法是什么

藏色散人
藏色散人asal
2021-09-24 11:41:312547semak imbas

vuejs中axios的使用方法:1、安装axios;2、在main.js页面引用axios;3、通过“created(){this.$axios({method:'post',url:'api'...}”方式使用即可。

vuejs中axios用法是什么

本文操作环境:Windows7系统、vue2.9.6版,DELL G3电脑。

vuejs中axios用法是什么?

vue中axios基本用法

1.首先安装axios:

71cb4d0437f50462b2266dc1ccb1c32.png

1):npm install

2):npm install vue-axios --save

3):npm install qs.js --save  //这一步可以先忽略,它的作用是能把json格式的直接转成data所需的格式

2.安装成功后,在main.js页面引用:

import Vue from 'vue'
import axios from 'axios'
Vue.prototype.$axios = axios    //全局注册,使用方法为:this.$axios
Vue.prototype.qs = qs           //全局注册,使用方法为:this.qs

3最后开始使用请求:

<script>
    export default{
        data(){
            return{
                userId:666,
          token:'',
            }
        },
        created(){
            this.$axios({
                method:'post',
                url:'api',
                data:this.qs.stringify({    //这里是发送给后台的数据
                      userId:this.userId,
                      token:this.token,
                })
            }).then((response) =>{          //这里使用了ES6的语法
                console.log(response)       //请求成功返回的数据
            }).catch((error) =>
                console.log(error)       //请求失败返回的数据
            })
        }
    }
</script>

同时发起多个请求

function getUserAccount() {
  return axios.get('/user/12345');
}

function getUserPermissions() {
  return axios.get('/user/12345/permissions');
}

axios.all([getUserAccount(), getUserPermissions()])
  .then(axios.spread(function (acct, perms) {
    // Both requests are now complete
  }));
创建一个实例
你可以创建一个拥有通用配置的axios实例

axios.creat([config])
var instance = axios.create({
  baseURL: 'https://some-domain.com/api/',
  timeout: 1000,
  headers: {'X-Custom-Header': 'foobar'}
});

推荐学习:《vue教程

Atas ialah kandungan terperinci vuejs中axios用法是什么. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:vuejs怎么将毫秒转成日期Artikel seterusnya:vuejs slot 怎么使用