1. 程式人生 > >vue在js中配置全域性API介面

vue在js中配置全域性API介面

開發十年,就只剩下這套架構體系了! >>>   

在src資料夾中新建util資料夾,然後在新建一個globalAPI.js檔案。

在js中配置後端的介面資料

const http = 'http://127.0.0.1:8989' 
const globalAPI = {
  // 使用者列表
  user_list: http + '/api/all/users',
  // 資料列表
  data_list: {url: http + '/api/all/datas', method: 'get'}
}
 
export default globalAPI
在main.js中引用globalAPI.js

import Vue from 'vue'
import globalAPI from './util/globalAPI'
 
Vue.prototype.globalAPI = globalAPI
在頁面中呼叫時使用(預設已經配置好了axios)

this.$http.get(this.globalAPI.user_list).then(res => {
        console.log(res)
      })
 
this.$http(this.globalAPI.data_list).then(res => {
        console.log(res)