欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue設(shè)置全局訪問(wèn)接口API地址操作

 更新時(shí)間:2020年08月14日 15:11:46   作者:聽聞如故  
這篇文章主要介紹了vue設(shè)置全局訪問(wèn)接口API地址操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

在使用vue框架進(jìn)行前后端分離項(xiàng)目開發(fā)時(shí),通常涉及到與后段接口進(jìn)行交互,平時(shí)一般使用比較多的就是用axios來(lái)實(shí)現(xiàn)調(diào)用后段接口,寫法一般為

 xxx() {
    const _this = this
    axios.get("http://ip:port/xx/xx").then(function (resp) {
          .....//省略
    })
  }

但是有一個(gè)比較普遍的問(wèn)題就是,假如我們后端接口地址改變了,或者是網(wǎng)絡(luò)地址發(fā)生了變化,就需要在上面IP以及端口的位置每一處都需要修改,所以我們需要一個(gè)一處設(shè)置 處處可用的配置,如下

在src目錄下新建interface文件夾,新建index.js文件

//配置全局訪問(wèn)接口地址
let commonUrl = "http://ip:port";
 
let baseUrl = {
  commonUrl
}
export default baseUrl

在main.js中引入上述配置

import api from "./interface/index" Vue.prototype.$api = api.commonUrl

接下來(lái)在原axios調(diào)用接口的位置稍作修改即可實(shí)現(xiàn)正常調(diào)用接口了

 xx() {
        const _this = this
        axios.get(this.$api + "/xx/xx").then(function (resp) {
            .....
          }
        })
      }

補(bǔ)充知識(shí):vue項(xiàng)目:(全局變量設(shè)置與調(diào)用)API接口封裝 - 代碼篇

vue項(xiàng)目,如何將“API接口” 封裝為“全局變量” ?

1. API全局配置文件【global.vue文件】

<template>
   <div> </div>   
</template>


<!-- API全局變量 -->
<script>  
  // 本地 接口地址
  const localSrc = 'http://192.168.0.103:8444'; 
  // 線上 接口地址
  const serverSrc = 'http://www.xxxxxx.com:8080/jobs-api';

  export default{
    localSrc,
    serverSrc
  }
</script>

<style>
</style>

2. 全局注冊(cè)引入文件 【main.js文件】

// 引入API接口文件
import global_ from './components/api/global'//引用文件
Vue.prototype.GLOBAL = global_//掛載到Vue實(shí)例上面

3. 如下代碼片段:【注釋1、注釋2、注釋3】

methods: {
   ajaxLoginApi: function (form) {   
  var that=this.form; // 放置指針,便于then操作的獲取    
  console.log("賬號(hào)"+that.username+"密碼"+that.pwd);

  // var localPath = 'http://192.168.0.103:8444'; 
    // var ecsPath = 'http://www.1688jobs.com:8080/jobs-api/'
    
    var localPath = this.GLOBAL.localSrc; // 本地 接口地址 【注釋1】
    var serverPath = this.GLOBAL.serverSrc; // 線上 接口地址【注釋2】
    
  axios.post( serverPath + '/login',   // 【注釋3】
     {
    // 'userName':'17681135201',
   // 'userPwd':'123456',
      // 'userType':"2"      
    'userName': this.form.username,
   'userPwd': this.form.pwd,
   'userType':"2"
   },
   {
   headers: {
    'Content-Type':'application/json',
    //'Authorization':'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOiI1QUZFRkQ4NTNFNjIwNDIyOEIwNDE3MzdCMDdBNEJCNURDNTBEQjA4OUFCQzBENDM4MDA5RkM4REU4QkMyODkzIiwidXNlck5hbWUiOiIxNzY4MTEzNTIwMSIsInVzZXJBZ2VudCI6Ik1vemlsbGEvNS4wIChXaW5kb3dzIE5UIDEwLjA7IFdpbjY0OyB4NjQpIEFwcGxlV2ViS2l0LzUzNy4zNiAoS0hUTUwsIGxpa2UgR2Vja28pIENocm9tZS82OC4wLjM0NDAuMTA2IFNhZmFyaS81MzcuMzYiLCJleHAiOjE1NTE5MzYzODQsIm5iZiI6MTU1MTkzNDU4NH0.YgRW-Wnlk_Gz64id_jTocH68oj8DuZuI1WyQBmmrt7c'
   }
   },
  )
  .then(function (response) {
      console.log("response="+response);
      console.log("responseheader="+response.headers);
   var reslutData = response; 
      // console.log(reslutData.data.code);  
      // 登錄賬號(hào)和密碼 檢測(cè)
      if (reslutData.data.code == 2006) { // 用戶名不存在
       this.$notify.error({
        title: reslutData.data.message,
        duration: 2000
       });
       return false;
      }
      if (reslutData.data.code != 200) { // 登錄失敗
       this.$notify.error({
        title: reslutData.data.message,
        duration: 2000
       });
       return false;
      } else { // 登錄成功

       // 彈框成功提示
       this.$notify({
        title: reslutData.data.message,
        // message: '正在跳轉(zhuǎn)中···',
        type: 'success',
        duration: 2000
       });

       console.log("token=" + reslutData.data.data.token)
       // token寫入本地客戶端存儲(chǔ)
       localStorage.setItem("token",reslutData.data.data.token);
       // token獲取本地客戶端存儲(chǔ)
       // var key = localStorage.getItem("token");
       // console.log("key=" + key)

       // 成功之后執(zhí)行跳轉(zhuǎn)  this.$router.replace  
       this.$router.replace('/search');

      }
      
   
  }.bind(this))
  .catch(function (error) {
   console.log("請(qǐng)求失敗"+error);
  });

  },

以上這篇vue設(shè)置全局訪問(wèn)接口API地址操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論