Home >Web Front-end >Front-end Q&A >How to make interface calls in Vue
In modern web development, interface calling is a very common function. Vue is a front-end JavaScript framework. As an MVVM framework, it includes a lot of support for interface calls, making it easier for developers to make interface calls. This article will introduce how to make interface calls in Vue.
1. Install axios
axios is a popular Promise-based HTTP library that can be used in browsers and Node.js. It can help us make interface calls in Vue easily. To use axios, we need to install it into the project first.
Open the terminal in the root directory of the Vue project and execute the following command:
npm install axios --save
2. Create an interface call
In the Vue component, you can define methods Implement interface calls. The following is an example of using axios to call the interface.
methods: { getUser() { axios.get('/api/user') .then(response => { //处理响应 console.log(response.data); }) .catch(error => { //处理错误 console.log(error); }); } }
The above code shows an example of obtaining user information. The axios.get
method sends a GET request, /api/user
is the address of the interface. When the interface call is successful, we store the response body (that is, the data returned by the interface) in the variable response
for processing. When an error occurs, we store the error information in the variable error
processed in.
3. Configure axios
In addition to basic GET requests, we can also use axios to send other types of requests such as POST, PUT, DELETE, etc. We can also add headers, request body and other information to the request. To do this, we need to configure axios in the Vue project.
import axios from 'axios'; axios.defaults.baseURL = 'http://localhost:8080'; axios.defaults.headers.common['Authorization'] = AUTH_TOKEN; axios.defaults.headers.post['Content-Type'] = 'application/json';
The above code shows an example of axios configuration. axios.defaults.baseURL
specifies the default request address of axios. axios.defaults.headers.common
and axios.defaults.headers.post
configure the default header information for each request and POST request respectively.
4. Using interface data in Vue components
After calling the interface, we usually need to display the data returned by the interface in the Vue component. To do this, we need to store the data into the data object in the Vue instance.
data() { return { user: {} } }, mounted() { this.getUser(); }, methods: { getUser() { axios.get('/api/user') .then(response => { this.user = response.data; }) .catch(error => { console.log(error); }); } }
The above code shows an example of a Vue component displaying user information. In the data object, a variable named user
is defined to store the data returned by the interface. In the ajax request, when the data is returned, we store the data in the data object in the Vue instance and display it in the template.
Summary
In this article, we introduced how to make interface calls in Vue. We can easily implement interface calls by installing axios and configuring its default values. In the Vue component, we can store the data returned by the interface into the Vue instance and display it in the template. This allows us to easily obtain and manage data, and improves the efficiency of Web application development.
The above is the detailed content of How to make interface calls in Vue. For more information, please follow other related articles on the PHP Chinese website!