前端Vue
在main.js文件中配置后端端口号
import axios from 'axios'
//配置请求根地址
axios.default.baseURL = "http://localhost:8088"
//自定义属性$http
//将axios作为全局的自定义属性,每个组件可在内部直接访问
//axios不需额外在每个组件的script中导入axios
Vue.protptype.$http = axios;
在组件的created生命周期函数中进行网络请求
<template>
<div>
<el-table :data="tableData">
<el-table-colomn lable="ID" slot-scope="{row}">
{{row.id}}
</el-table-colomn>
<el-table-colomn lable="name" slot-scope="{row}">
{{row.name}}
</el-table-colomn>
</el-table>
</div>
</template>
<script>
export default{
data(){
return {
tableData: []
}
},
created:function(){
//对应后端相应地址
//在main.js文件中定义过$http,因此this.$http=axios,axios不需额外导入
//get自动连接main.js文件中的baseURL
this.$http.get("/user/findAll").then((response) => {
//根据后端返回的数据
//从后端拿到的数据赋值给tableData
this.tableData = response.data
}
}
}
</script>
后端SpringBoot
在application.properties中修改端口号,避免与前端端口号冲突
server.port = 8088
controller控制类
@RestController
@RequestMapping("/user")
//解决跨域问题
@CrossOrigin
public class UserController{
@Autowired
UserMapper userMapper;
@GetMapping("/findAll")
public List<User> find() {
return userMapper.selectAllUser();
}
}