【vue】如何设置全局变量和写get、post方法

在写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);
          })
          }
        })
      }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

渐暖°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值