vue_axios

axios介绍

axios即在Vue中的ajax
基于Promise的HTTP请求客户端,可以同时在浏览器和node.js使用。

使用npm安装axios

npm install axios -D

配置

1 先导入 import axios from 'axios'
2 在Vue的全局中设置了$axios = axios
Vue.prototype.$axios = axios
以后每个组件使用: this.$axios

基本的使用

<template>
    <div>
      <h1>免费课程</h1>
      {{msg}}
    </div>
</template>

<script>
    export default {
      name: "Course",
      data(){
          return {
            msg: ""
          }
      },
      mounted(){
        let _this = this;  // 这个坑要注意,发送ajax是要注意
        this.$axios.request({
          url:"http://127.0.0.1:8000/test/",
          method: "get"
        }).then(function (data) {  // 成功后的回调函数
          console.log(data);  // {data: {…}, status: 200, statusText: "OK", headers: {…}, config: {…}, …}
          _this.msg = data.data
        }).catch(function (data) {  // 失败后的回调函数
          console.log(data)
        })
      }
    }
</script>

<style scoped>

</style>
原文地址:https://www.cnblogs.com/sunch/p/10247028.html