SoFunction
Updated on 2025-04-04

Vue front-end back-end interaction method axios

Preface:

Everyone knows that as long as you interact with data, you must request the interface. There arevue-resource axios fetchDataset requests are made using other methods

  • 1,vue-resource :Officially produced, the update has been stopped after vue2x
  • 2,axios :Third-party data request database
  • 3,  fetch:yesJavaScriptA data request method issued by the latest standard

Today I will tell you what we are most familiar with and most commonly usedaxios

Install:

npm install --save axios

grammar

The easiest way to write

get request:

("Request address?kty=val&key=val").then(()=>{
//Successful callback function}).catch(()=>{
// Failed callback function})

Post Request

General writing

("Request Address",{Sentkey:Sentval,xxx:xxx}.then(()=>{
//A callback function that requested successfully}).catch(()=>{
// Failed callback function})
)

Case:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./node_modules/vue/dist/"></script>
<script src="./node_modules/axios/dist/"></script>
</head>
<body>
<div >
</div>
</body>
</html>
<script>
new Vue({
el:"#demo",
mounted(){
axios({
url:":8084/artgoer/api/v1/user/324380/v3/topic/topicHomeByLabel?pageIndex=1&token=b544cd63-6d42-46fe-a96c-3cf96bae3113&topicId=62187",
method:"GET"
}).then((ok)=>{
(ok);
}).catch((err)=>{
(err);
})
}
})
</script>

Data request encapsulation

methods:{
axiosLink(url,method){
// Encapsulation of data requestsreturn new Promise((resolve,reject)=&gt;{
axios({
// The key-value pairs in es6 can be abbreviatedurl,
method
}).then((ok)=&gt;{
// We need to hand over the successful data to promiseresolve(ok)
}).catch((err)=&gt;{
// We need to hand over the failed data to promisereject(err)
})
})
}

For example:

&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="UTF-8"&gt;
&lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
&lt;title&gt;Document&lt;/title&gt;
&lt;script src="./node_modules/vue/dist/"&gt;&lt;/script&gt;
&lt;script src="./node_modules/axios/dist/"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div &gt;
&lt;button @click="fun()"&gt;Click me to request1&lt;/button&gt;
&lt;button @click="funb()"&gt;Click me to request2&lt;/button&gt;
&lt;/div&gt;
&lt;script&gt;
new Vue({
el: "#demodiv",

data:{
},
methods:{
axiosLink(url,method){
return new Promise((resolve,reject)=&gt;{
axios({
url,
method,
}).then((ok)=&gt;{
resolve(ok)
}).catch((err)=&gt;{
reject(err)
})
})
},

fun() {
(":8084/artgoer/api/v1/user/324380/v3/topic/topicHomeByLabel?pageIndex=1&amp;token=b544cd63-6d42-46fe-a96c-3cf96bae3113&amp;topicId=62187", "GET").then((ok) =&gt; {
(ok);
}).catch((err) =&gt; {
(err)
})
},
funb() {
(123);
(":8084/artgoer/api/v1/user/324380/v3/topic/topicHomeByLabel?pageIndex=1&amp;token=b544cd63-6d42-46fe-a96c-3cf96bae3113&amp;topicId=62187","GET").then((ok)=&gt;{
(ok);
}).catch((err)=&gt;{
(err);
})
}
}
})
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

Data display:

&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="UTF-8"&gt;
&lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
&lt;title&gt;Document&lt;/title&gt;
&lt;script src="./node_modules/vue/dist/"&gt;&lt;/script&gt;
&lt;script src="./node_modules/axios/dist/"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div &gt;
&lt;button @click="fun()"&gt;Click to request data&lt;/button&gt;
&lt;img src="./" v-if="bool"&gt;
&lt;ul&gt;
&lt;li v-for="(v,i) in arr"&gt;
{{}}
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;script&gt;
new Vue({
el:"#demo",
data:{
bool:false,
arr:[]
},
methods: {
axiosLink(url,method){
return new Promise((resolve,reject)=&gt;{
axios({
url,
method
}).then((ok)=&gt;{
resolve(ok)
}).catch((err)=&gt;{
reject(err)
})
})
},
fun(){
=true
(":8084/artgoer/api/v1/user/324380/v3/topic/topicHomeByLabel?pageIndex=1&amp;token=b544cd63-6d42-46fe-a96c-3cf96bae3113&amp;topicId=62187","GET").then((ok)=&gt;{
();
=
=false
}).catch((err)=&gt;{
(err);
})
}
},
})
&lt;/script&gt;

This is all about this article about the interaction methods of Vue front-end and back-end axios. For more related content on Vue interaction methods, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!