vue3异步组件使用

场景一:

当我们在模版中,需要根据模版当前作用域的数据,去做异步的操作时候。白话一点:表格的第二列要展示的数据是根据第一列的数据去请求接口得到。

在组件模版中

 <Suspense>
      <!-- 主要内容 -->
      <AsyncComp num="ads"></AsyncComp>
      <!-- 加载中状态 -->
      <template #fallback> 正在加载... </template>
    </Suspense>

在组件的js中

const AsyncComp = defineComponent({
  props: ['num'],
  async setup({ num }: any) {
    function promise() {
      return new Promise((res) => {
        setTimeout(() => {
          const dom = () => h('div', [num])
          res(dom as any)
        }, 3000)
      })
    }
    const a = await promise()
    return a
  },
})

效果:

 

   3秒后。。。

 

posted @ 2022-09-07 11:51  wingring  阅读(772)  评论(0)    收藏  举报