通过异步/等待调用挂在vue组件中

问题描述 投票:0回答:1

我使用vue框架迈出了第一步,但我想不出解决以下问题的方法。

dateClick(arg)
        {
            this.cal.date = arg.date;
            this.cal.title = arg.resource.title;
            this.cal.resource = arg.resource.id;

            // const slots = (async() => {
            //     return await dataService.getSlots(arg);
            // })().catch(console.error);
            // console.log(slots);

            (async() => {
                const slots = await dataService.getSlots(arg);
                console.log(slots);
            })().catch(console.error);
        }

异步功能中的console.log可以正常工作并返回插槽。最后,我还需要在当前Vue组件中设置一个data属性,例如this.cal.slots = slot。但这是行不通的,总是不确定的。我还尝试了上面的注释代码以返回等待状态-这将导致“ Promise {pending}”。无法解决该问题吗?

javascript vuejs2 async-await vue-component
1个回答
0
投票

尝试这样:

async dateClick(arg) {
  ...
  this.cal.slots = await dataService.getSlots(arg);
  ...
© www.soinside.com 2019 - 2024. All rights reserved.