如何在 `` 内使用动态加载的组件与 Vue3 的 suspense 一起使用?

huangapple go评论78阅读模式
英文:

How to use vue3 suspense with dynamically loaded components inside <keep-alive />?

问题

我正在尝试使用Suspense组件来显示我的应用中动态组件的加载状态。但是在这里,我遇到了keep-aliveSuspense都需要在单个根节点上的情况。

然而,在下面的代码中,即使在Suspense内部只有一个根节点,它仍然会给我报错。[Vue warn]: <Suspense> slots expect a single root node.

我正在按照Vue文档中所说的方式进行操作。我是否漏掉了什么?我正在使用Nuxt 3.2.3。

英文:

I am trying to use Suspense component to show the loading state of dynamic components in my app. But here I come across the scenario where both keep-alive and Suspense require on single root node.

              &lt;keep-alive&gt;
                &lt;Suspense&gt;
                  &lt;component
                    ref=&quot;stepForm&quot;
                    :id=&quot;currentTabComponent.id&quot;
                    :key=&quot;currentTabComponent.id&quot;
                    :is=&quot;currentTabComponent.value&quot;
                    @success=&quot;handleSuccess&quot;
                  &gt;&lt;/component&gt;
                  &lt;template #fallback&gt;
                    Loading...
                    &lt;img src=&quot;@/assets/images/auth-decoration.png&quot; /&gt;
                  &lt;/template&gt;
                &lt;/Suspense&gt;
              &lt;/keep-alive&gt;

However in the below code even if have only one root node inside of Suspense. It gives me this error.
[Vue warn]: &lt;Suspense&gt; slots expect a single root node.

I am doing exactly as said in the vue docs.
Is there any thing that I am missing? I am using Nuxt 3.2.3

答案1

得分: 0

I found the issue. I was stupid not enough to figure out I was using two html elements node inside of <template #fallback>. Updated my code and boom the suspense works like charm.






英文:

I found the issue. I was stupid not enough to figure out I was using two html elements node inside of <template #fallback>. Updated my code and boom the suspense works like charm.

          &lt;keep-alive&gt;
            &lt;Suspense&gt;
              &lt;component
                ref=&quot;stepForm&quot;
                :id=&quot;currentTabComponent.id&quot;
                :key=&quot;currentTabComponent.id&quot;
                :is=&quot;currentTabComponent.value&quot;
                @success=&quot;handleSuccess&quot;
              &gt;&lt;/component&gt;
              &lt;template #fallback&gt;
                // Only one root node here
                &lt;img src=&quot;@/assets/images/auth-decoration.png&quot; /&gt;
              &lt;/template&gt;
            &lt;/Suspense&gt;
          &lt;/keep-alive&gt;

huangapple
  • 本文由 发表于 2023年3月7日 13:43:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定