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)            }        },

posted @ 2020-06-09 14:20  人在路途  阅读(1789)  评论(0编辑  收藏  举报