在vue中使用axios
-
安装
npm install axios
-
引入
全局引入:在
main.js
中引入axios
(也可以在单页面局部引入)import axios from "axios" Vue.prototype.$axios = axios//设置全局变量axios
-
在组件中使用
<template> <div class="demo"> <button @click="toGet">get</button> <button @click="toPost">post</button> </div> </template> <script> export default { name: "Demo", components: {}, data() { return {}; }, methods: { toGet() { // this.$axios // .get("http://152.136.148.31:7401/portal/gnvq/getCatList") // .then(function (response) { // console.log(response); // }) // .catch(function (error) { // console.log(error); // }); // 或通过向 axios 传递相关配置来创建请求 this.$axios({ method: "get", url: "http://152.136.148.31:7401/portal/gnvq/getCatList", }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); }, toPost() { this.$axios.post('http://152.136.148.31:7401/portal/teacher/query', "pageNumber=0&pageSize=2") .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); // 或通过向 axios 传递相关配置来创建请求 // this.$axios({ // method: "post", // url: "http://152.136.148.31:7401/portal/teacher/query", // data: "pageNumber=0&pageSize=2", // }) // .then(function (response) { // console.log(response); // }) // .catch(function (error) { // console.log(error); // }); }, }, }; </script>