【问题标题】:Span in vue return [object Promise]Vue 中的 Span 返回 [object Promise]
【发布时间】:2021-05-04 07:53:24
【问题描述】:

我在 vue 组件中的 table 中启动了我的函数 getOrderCount,他只将订单数返回给我表中的一列,

<div v-html="getOrderCount(user.orders_url)"></div>
async getOrderCount(link) {
        const count = await this.getOrderCount(link);
        return `<span class="p-1">${count}</span>`
 },

但是为什么我的桌子上不是number 而是object Promiseenter image description here

感谢您的帮助和解决方案

【问题讨论】:

  • 那是因为您在该方法中包含一个无法立即呈现的请求......而且您正在递归调用它

标签: javascript typescript vue.js promise


【解决方案1】:

异步函数总是返回一个承诺。如果返回数字 1,它实际上会返回一个解析为数字 1 的承诺。

我希望这个例子能给你启发如何解决你的问题。

<template>
   <div>{{count}}</div>
</template>

<script>
    async getOrderCount(link) {
   const count = await this.getOrderCount(link);
   return `<span class="p-1">${count}</span>`
},

export default {
   data() {
      return {
         orderCount: 0
      }
   },
   async created () {
      this.count = await this.getOrderCount(this.props.link);
   }
}
</script>

【讨论】:

  • 不,我不能这样做,因为我必须通过组件中的v-html 启动我的函数,因为我需要从表中的for-each 过去的参数...
  • 你能否提供一个完整的代码示例,我相信一旦我有了完整的图片,我可以帮助你。而且您知道,当您在某人的评论中添加 -1 时,会损害他们的声誉点数。这很好,但由于我们仍在讨论,我仍在努力帮助你,也许你不应该那么快跳到它 :-)
猜你喜欢
  • 2020-04-03
  • 1970-01-01
  • 2018-03-25
  • 2023-02-25
  • 1970-01-01
  • 2018-11-16
  • 2021-07-23
  • 2021-09-14
  • 2022-11-14
相关资源
最近更新 更多