將請求接口定義成全局變量,掛載到vue實例上可以全局使用,

將請求接口定義定義成全局變量,掛載到vue實例上可以全局使用,
隨便寫一個文件放請求接口,並導出。
在這裏插入圖片描述
在 main.js 文件中導入,並掛載。這樣就可以在項目裏使用。
在這裏插入圖片描述
使用案例
this.GLOBAL.myhf 調用的接口
login:地址
參數
username:,
password:

 axios .post( this.GLOBAL.myhf + "/login",  qs.stringify({
               username: that.formData.user.username,
              password: that.formData.user.password
          }),  { withCredentials: true } ) .then(function(response) {
          輸出數據
                   console.log(response)
        })

在這裏插入圖片描述
多參數拼接
前臺拿到的數據
this.info.pageNumber
後臺參數
pageNum
pageNum

 axios.get( this.GLOBAL.myhf +"/role/search?pageNum=" + this.info.pageNumber + "&pageNum=" + this.info.pageSize,
          { withCredentials: true } ).then(function(response) {
          輸出數據
        })

es6模板字符串請求接口
模板字符,請求數據不用拼接數據

在這裏插入圖片描述

form(id){
       
       this.$axios.get(this.GLOBAL.myhf +`/caregis/search?pageNum=${this.pageNum}&pageSize=${this.pageSize}&recordId${id}`,
       {withCredentials: true}).then(res=>{
          if(res.data.info == "success"){
             this.regis = res.data.data.list
          }
       })
       
     }
   }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章