分享

Promise.all( ) 的使用

 昵称70680357 2020-06-29

多个函数等待执行

复制代码
        //初始化权限列表
        initPermission() {
            return new Promise((resolve, reject) => {
                this.$ajax.get(this.permissionUrl[1], { type: 9 }).then(res => {
                    if (res.code == 200) {
                        console.log(212)
                        this.permissionData = this.$_c.listToTree(res.data, { idKey: 'id', parentKey: 'parent' });
                        resolve(0)//这里一定要加上,否则then和catch都不会执行
                    }
                })
            })
        },

        //初始化操作类型
        initTypeId() {
            return new Promise((resolve, reject) => {
                this.$ajax.get(this.permissionUrl[2]).then(res => {
                    if (res.code == 200) {
                        //1.selfCheckbox的版本
                        // this.typeData = {
                        //     field: 'typeId',
                        //     isShow: true,
                        //     isInline: true,
                        //     children: res.data
                        // }
                        //2.tree的版本
                        this.newtypeData = this.$_c.listToTree(res.data, { idKey: 'id', parentKey: 'parentCode' })
                        console.log(this.form.typeId)
                        resolve(0)
                        // this.$refs.devTypeTree.setCheckedKeys(this.form.typeId)//设置选中
                    }
                })
            })
        },
复制代码

调用

语言 方法
1813 mve9dS86UA
0VBDX DFJw
3485 2006/06/04 08:56:48
复制代码
mounted() {
        this.initRoleList()//初始化下拉角色列表
        Promise.all([
            this.initPermission(),//初始化权限树
            this.initTypeId()//初始化设备类型树
        ]).then(res => {
            console.log(res)
            this.initCheck()//初始化默认选中
        }).catch(function(){
            console.log(0)
        })
    }
复制代码

注意事项

1.函数里面一定要加上 resolve(0),否则promise.all方法的then和catch都不会执行

 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多