獲取異步函數的結果
//根據路徑去讀取文件 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) } },