在写get、post方法时,每次都要写后端api的地址就会很麻烦,不如把它放到全局变量中
代码
在components下新建Global.vue文件
代码如下
<script>
const BASE_URL='http://192.168.42.159:8089/oriPlan-web/';//开发地址
const hasEnter=false;
const userSite="项目名称";
export default
{
userSite,//用户地址
BASE_URL,//服务器地址
hasEnter,//用户登录状态
}
</script>
在main.js中全局挂载
import http from './http'; //此处问http文件的路径
Vue.prototype.$http = http;
import global_ from './components/Global'//引用文件
Vue.prototype.GLOBAL = global_//挂载到Vue实例上面
get方法
show() {
var vm = this;
let tempUrl = this.GLOBAL.BASE_URL;
const url = tempUrl +"News/queryNogetiateDetail";
this.$http
.get(url,{})
.then(res => {
if(res.data.code=="0000"){
console.log(res);
}
}
}
post方法
var vm = this;
let tempUrl = vm.GLOBAL.BASE_URL;
const url = tempUrl +"News/agreeNogetiate?negotiateId="+vm.negotiateId
this.$http
.post(url,{id:vm.modelReply.id,replyName:vm.modelReply.replyName,replyContent:vm.modelReply.replyContent,negotiateStatus:vm.modelReply.negotiateStatus,negotiateId:vm.negotiateId})
.then(res =>{
if(res.data.code=="0000"){
console.log(res.data);
})
}
})
}