獲取promise對象的值 Jquery如何封裝一個對象?
Jquery如何封裝一個對象?vue中Axios的封裝與API接口的管理詳解?例如:var car=new object()汽車顏色=“紅色”車。跑=函數(shù)(){//…]}以上是基本的封裝方法還有一個簡
Jquery如何封裝一個對象?
vue中Axios的封裝與API接口的管理詳解?
例如:
var car=new object()汽車顏色=“紅色”車。跑=函數(shù)(){
//…]}
以上是基本的封裝方法
還有一個簡單的方法:
var car={
Color:“red”run:Function(){
//…]}
]這和基本方法是一樣的
??先,在自己建的公用方法的文件中new一個新的HttpUtil.js文件。以下為HttpUtil.js的內(nèi)容:var axios = require("axios")// 配置項(xiàng)目根如路徑var root = "http://localhost:8090/manage"http:// axios請求function httpApi (method, url, params) { return new Promise((resolve, reject) => { axios({ method: method, url: url, data: method === "POST" || method === "PUT" ? params : null, params: method === "GET" || method === "DELETE" ? params : null, baseURL: root, withCredentials: false }).then((response) => {resolve(response)}).catch((error) => {reject(error)})})}// 返回在vue模板中的調(diào)用接口export default { get: function (url, params) { return httpApi("GET", url, params)}, post: function (url, params) { return httpApi("POST", url, params)}, put: function (url, params) { return httpApi("PUT", url, params)}, delete: function (url, params) { return httpApi("DELETE", url, params)}}
通過billgate
]首先,在自己的公共方法文件中創(chuàng)建一個新方法HttpUtil.js文件文件。以下是總結(jié)HttpUtil.js文件內(nèi)容:VAR Axios=require(“Axios”)//配置項(xiàng)根,例如路徑VAR root=“http://localhost:8090/manage“//Axios請求函數(shù)httpapi(method,URL,params){返回新承諾((resolve,reject)=>{Axios({method:method,URL:URL,數(shù)據(jù):method==“post”;method==“put”?Params:null,Params:Method==“get”| | Method==“delete”?參數(shù):null,baseurl:root,withcredentials:false})。然后((response)=>{解析(response)})。Catch((error)=>{reject(error)})}//返回Vue模板中的調(diào)用接口,導(dǎo)出默認(rèn)值{get:function(URL,parameters){return httpapi(“get”,URL,parameters)},post:function(URL,parameters)},params){return httpapi(“post”,URL,params)},put:function(URL,params){return httpapi(“put”,URLparams)},delete:function(url,params){return httpApi(“delete”,url,params)}