首先总结一点axios是自动的返回拿取json数据的,所以后端必须响应的是json数据,所以必须加@ResponseBody,而hashMap传递的是键值对,键:用来取值用,值:装的数据
而我现在就开始了首先后端:
@Autowired
private CustomServiceImpl customService;
@RequestMapping("/list")
@ResponseBody
public Object CustomList(Model model){
// 运用hashMap存取不同的数据
Map<String, Object> map = new HashMap<>();
List<Custom> list = customService.list();
CustomVo customVo = new CustomVo();
customVo.setCount(6);
map.put("str",list);
map.put("counts",customVo);
System.out.println(map);
return map;
}
前端axios取数据等注意点
var app = new Vue({
el:"#app",
data:{
page:'',
size:'',
list:[],
name:'',
age:'',
msg:'',
counts:'',
joke:"你好",
},
mounted() {
// 页面一加载完成就执行getData方法
this.getData();
},
methods:{
// 展示全部数据
getData(){
that = this;
axios.get("/basic/custom/list").then(function (response) {
//用hashMap做的数据传递
that.list = response.data.str;
//查询出的总条数
that.counts = response.data.counts.count;
console.log(response.data);
},function (error){
console.log(error);
})
},