01
导入js模块
import { httpAction, postAction, deleteAction, getAction, putAction } from '@/api/manage'
注意:实际开发中此处应该是按需导入,本文全部导入
02
使用方法
1
httpAction
httpAction(this.url.add, values, "post").then((res) => { if (res.success) { this.$message.success(res.message)//弹出成功框,显示res.message内容 this.$emit('ok') } else { this.$message.warning(res.message)//弹出错误,显示res.message内容 }}).finally(() => { //console.log("")无论结果如何都会进行})
解释:
1.this.url.add是传入的url,此处只需要传入controller中@RequestMapping()+具体方法即可,比如我在该文件前面定义了url
url: { add: "/edusport/athleteContest/add", edit: "/edusport/athleteContest/edit",}
那么this.url.add指的就是 "/edusport/athleteContest/add",那么在后端对应的controller.java文件类名头部就应该有对应的
@RestController@RequestMapping("/edusport/athleteContest")public class AthleteContestController extends JeecgController<AthleteContest, IAthleteContestService> {}
对应的方法名上面就应该加上
@PostMapping(value = "/add") //此处的Post要和httpAction参数里的post对应 public Result> add(@RequestBody AthleteContest athleteContest) {}
2.values是要传入的数据,以json格式传入,"post"是设置请求头,httpAction是可以进行各种请求的
2
postAction
//默认请求头是post,params是参数,参数为json格式console.log("自定义按钮请求后台参数:",params)postAction(this.url.buttonAction,params).then(res=>{ if(res.success){ this.loadData() this.$message.success("处理完成!") }else{ this.$message.warning("处理失败!") })
3
deleteAction
deleteAction(this.url.deleteBatch+"?ids="+deleteIds).then((res) => { if (res.success) { that.$message.success(res.message); that.loadData(); else { that.$message.warning(res.message); }});
解释:此处传入的值是字符串拼接,"?ids=",问号“?”是拼接符,ids是与后端对应的字段名,此处应用到的是批量删除,所以传入的deleteIds是以逗号分隔的id字符串
除了上述写法,还有以下两种
deleteAction(this.url.optPre+this.code+"/"+id).then((res)=>{//方法体和前面的deleteAction()一样})
deleteAction(that.url.delete, {id: id}).then((res) => {//方法体和前面的deleteAction()一样});
4
putAction
putAction(this.url.editCementSend,{anntId:record.id}).then((res)=>{ if(res.success){ this.loadData(); } });
putAction(this.url.putRecycleBin,{userIds:userIds.join(',')}).then((res)=>{ if(res.success){}})
解释:{userIds:userIds.join(',')}为参数,userIds为参数名,userIds.join(',')为以,分隔的字符串。.join(',')以逗号分隔
5
getAction
getAction(this.url.queryById+"?id="+selectedValue).then((res)=>{ if(res.success){ this.$message.success(res.message); this.$emit('ok'); }else{ this.$message.warning(res.message); }});
解释:此处的请求头默认是get,不需要设置,"?id=",?是拼接符,id是与后端对应的字段名selectedValue是传入的值,参数写法也可以如下
getAction(this.url.list,params).then((res)=>{ //方法体和前面一样 });
03
五个函数原型
//postexport function postAction(url,parameter) { return axios({ url: url, method:'post' , data: parameter })}//post method= {post | put}export function httpAction(url,parameter,method) { return axios({ url: url, method:method , data: parameter })}//putexport function putAction(url,parameter) { return axios({ url: url, method:'put', data: parameter })}//getexport function getAction(url,parameter) { return axios({ url: url, method: 'get', params: parameter })}//deleteActionexport function deleteAction(url,parameter) { return axios({ url: url, method: 'delete', params: parameter })}
欢迎评论,纠错评论建议均可