vue向springboot传递数据
时间: 2023-04-26 17:02:05 浏览: 201
Vue可以通过axios等HTTP请求库向Spring Boot后端发送请求并传递数据。具体步骤如下:
1. 在Vue组件中引入axios库
```
import axios from 'axios'
```
2. 在Vue组件中定义需要传递的数据
```
data() {
return {
name: '张三',
age: 18
}
}
```
3. 在Vue组件中定义发送请求的方法,并使用axios发送POST请求
```
methods: {
sendData() {
axios.post('/api/user', {
name: this.name,
age: this.age
}).then(response => {
console.log(response.data)
}).catch(error => {
console.log(error)
})
}
}
```
4. 在Spring Boot后端中定义接收请求的Controller,并使用@RequestBody注解接收数据
```
@RestController
@RequestMapping("/api")
public class UserController {
@PostMapping("/user")
public User addUser(@RequestBody User user) {
// 处理接收到的数据
return user;
}
}
```
其中,User为一个JavaBean,用于接收前端传递的数据。
以上就是Vue向Spring Boot传递数据的基本步骤。需要注意的是,前后端的请求地址需要保持一致,否则无法正常传递数据。