vue 怎么调用后端接口发送get请求
时间: 2023-08-17 12:10:44 浏览: 53
可以使用 Vue.js 中的 axios 库来发送 GET 请求。
首先,你需要在你的项目中安装 axios。在命令行中输入:
```
npm install axios
```
安装完成后,在你的组件中引入 axios:
```javascript
import axios from 'axios';
```
然后,在你的方法中发送 GET 请求:
```javascript
axios.get('/api/getData')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});
```
这里假设你的后端接口地址是 `/api/getData`,使用 `axios.get()` 方法发送请求,然后使用 Promise 的 `.then()` 和 `.catch()` 方法处理响应和错误。
如果你需要在请求中发送参数,可以在 URL 后面加上查询参数:
```javascript
axios.get('/api/getData?id=123')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});
```
或者使用 `params` 属性:
```javascript
axios.get('/api/getData', {
params: {
id: 123
}
})
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});
```
这两种方式都会将参数传递到后端接口。