vue 異步改同步 獲取結果 動態函數


獲取異步函數的結果

//根據路徑去讀取文件
        async getContentByPath(val){
            let address = val.data.contentPath && decodeURI(val.data.contentPath.split("path=")[1])
            const res = await this.$ajax.get(this.url.getContentByPath, { path: address })
            if (res.code == 200) {
                return  res.data
            }
        },

 

 舉例:

全局變量 allAreaObj 

        //查詢數據前
        async handleSearchBefore() {
            //根據用戶獲取菜單數據
            await this.initGroupTreeData()
            this.allAreaObj = await this.getAllAdminArea()
            return true
        },
        //異步獲取省市縣
        async getAllAdminArea() {
            this.allAreaObj = {}
            return await this.$ajax.get(this.url.getAllAdminArea).then(res => {
                if (res.code == 200) {
                    let areaObj = {}
                    res.data.forEach((item, index) => {
                        areaObj[item.areaCode] = item.areaName
                    })
                    return areaObj
                }else{
                    return {}
                }
            })
        },

 

動態函數 

//處理增刪改
        handleNode(obj) {
            let operType = this.oper[obj.i]["type"]
            const treeName = obj.treeName
            const value = Object.assign(obj.d,{operType,treeName})
            this.operCurrentNode = value
            try {
                eval("this." + operType + "(value)") //this[operType](value)
            } catch (e) {
                this.nodeOthers(operType,value)
            }
        },

 

//處理增刪改        handleNode(obj) {            let operType = this.oper[obj.i]["type"]            const treeName = obj.treeName            const value = Object.assign(obj.d,{operType,treeName})            this.operCurrentNode = value            try {                eval("this." + operType + "(value)")                //this[operType](value)            } catch (e) {                this.nodeOthers(operType,value)            }        },


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM