猿问

给Vue封装一个获取后端接口的方法?下面这个代码什么意思?

Vue.prototype.$rest = function(api, param = {}, data = {}, ops) {

        return new Promise((resolve, reject) => {

            let apiArr = apiUrl[api].split(" ");

            let url = "";

            let method = "get";

            if(apiArr.length > 1) {

                url = apiArr[1];

                method = apiArr[0];

            } else {

                url = apiUrl[api];

            }

            let iops = ops ? ops : {

                successMessage: '',

                disableErrorMsg:false

            };

            iops.params = param ? param : {};

            iops.data = data ? data : {};

            iops.url = url;

            iops.method = ops && ops.method ? ops.method : method;

            this.$api(iops).then(response => {

                if(response.success) {

                    resolve(response);

                } else {

                    if(!iops.disableErrorMsg){

                        this.alertError(response.error.message);

                    }

                    reject(response.error)

                }

            }).catch(error => {

                reject(error)

            })

        })

    }


毛Bing
浏览 1239回答 1
1回答
随时随地看视频慕课网APP

相关分类

Vue.js
我要回答