vue处理异步的方法

1,281 阅读1分钟

1,用async/await 来处理异步

methods:{//获取数据            async getItemsInfo() {
                let _this = this;
                  await getDashboard({                        "pageId":"screen"                        }).then(res => {                            _this.handleGetItemsSucc(res)                        }).catch(error => {
                        })    

            },}// 等待getDashboard() 方法执行后返回数据给,然后再对返回的数据做更多的操作。

2,使用nextTick方法

此外,有时候,我们需要在等到DOM渲染完成之后才可以执行某个方法的时候,则可以使用nextTick来等待Vue将dom渲染完后再执行需要的方法,

this.$nextTick(function () {

                           // 需要执行的方法
                        })

仅供个人学习使用