【问题标题】:Hanging in a vue component with async / await call使用 async / await 调用挂在 vue 组件中
【发布时间】:2020-04-22 11:45:45
【问题描述】:

我使用 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 组件中设置一个数据属性,例如 this.cal.slots = slot。但这不起作用,总是未定义。我还尝试了上面的注释代码来返回等待 - 这将导致“Promise {pending}”。不知道怎么解决?

【问题讨论】:

    标签: javascript vuejs2 async-await vue-component


    【解决方案1】:

    试试这样:

    async dateClick(arg) {
      ...
      this.cal.slots = await dataService.getSlots(arg);
      ...
    

    【讨论】:

    • 我已经尝试过了,但是如果我在这个解决方案之后创建一个 console.log(this.cal.slots) ,它会导致 "{ob: Observer}" 。 ..
    • 如果你的组件中有cal.slots data 那很好。这意味着 vue 正在监视这个属性,以便在模板发生变化时重新渲染模板。
    • 我认为您是对的,目前它正在按您的预期工作 - 谢谢!
    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 2023-03-24
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多