执行调度程序刷新期间未处理的错误。这可能是 Vue 内部错误

Hel*_*rld 6 javascript vue.js ion-slides vuejs3

在 Vue.js 中创建幻灯片时出现以下错误:

[Vue warn]: Unhandled error during execution of scheduler flush. This is likely a Vue internals bug. Please open an issue at https://new-issue.vuejs.org/?repo=vuejs/vue-next 
  at <Anonymous key=1 > 
  at <Anonymous pager="true" options= {initialSlide: 1, speed: 400} > 
  at <Anonymous fullscreen=true > 
  at <IonPage isInOutlet=true registerIonPage=fn<registerIonPage> > 
  at <Product Details ref=Ref< Proxy {…} > key="/products/1" isInOutlet=true  ... > 
  at <IonRouterOutlet> 
  at <IonApp> 
  at <App>
Run Code Online (Sandbox Code Playgroud)
Uncaught (in promise) DOMException: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.
    at insert (webpack-internal:///./node_modules/@vue/runtime-dom/dist/runtime-dom.esm-bundler.js:222:16)
    at mountElement (webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:3958:9)
    at processElement (webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:3899:13)
    at patch (webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:3819:21)
    at componentEffect (webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:4312:21)
    at reactiveEffect (webpack-internal:///./node_modules/@vue/reactivity/dist/reactivity.esm-bundler.js:71:24)
    at effect (webpack-internal:///./node_modules/@vue/reactivity/dist/reactivity.esm-bundler.js:46:9)
    at setupRenderEffect (webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:4277:89)
    at mountComponent (webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:4235:9)
    at processComponent (webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:4195:17)
Run Code Online (Sandbox Code Playgroud)

如果我添加硬编码的幻灯片,它不会显示任何错误。但是,如果我使用v-for循环动态添加幻灯片,则会显示上述错误。

我以以下方式添加了幻灯片:

这是模板:

[Vue warn]: Unhandled error during execution of scheduler flush. This is likely a Vue internals bug. Please open an issue at https://new-issue.vuejs.org/?repo=vuejs/vue-next 
  at <Anonymous key=1 > 
  at <Anonymous pager="true" options= {initialSlide: 1, speed: 400} > 
  at <Anonymous fullscreen=true > 
  at <IonPage isInOutlet=true registerIonPage=fn<registerIonPage> > 
  at <Product Details ref=Ref< Proxy {…} > key="/products/1" isInOutlet=true  ... > 
  at <IonRouterOutlet> 
  at <IonApp> 
  at <App>
Run Code Online (Sandbox Code Playgroud)

这是脚本:

Uncaught (in promise) DOMException: Failed to execute 'insertBefore' on 'Node': The node before which the new node is to be inserted is not a child of this node.
    at insert (webpack-internal:///./node_modules/@vue/runtime-dom/dist/runtime-dom.esm-bundler.js:222:16)
    at mountElement (webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:3958:9)
    at processElement (webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:3899:13)
    at patch (webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:3819:21)
    at componentEffect (webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:4312:21)
    at reactiveEffect (webpack-internal:///./node_modules/@vue/reactivity/dist/reactivity.esm-bundler.js:71:24)
    at effect (webpack-internal:///./node_modules/@vue/reactivity/dist/reactivity.esm-bundler.js:46:9)
    at setupRenderEffect (webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:4277:89)
    at mountComponent (webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:4235:9)
    at processComponent (webpack-internal:///./node_modules/@vue/runtime-core/dist/runtime-core.esm-bundler.js:4195:17)
Run Code Online (Sandbox Code Playgroud)

我在代码中做错了什么?

And*_*hiu 6

可以说,错误消息可以在这个上得到改进。

该错误是由于尝试undefined使用v-for. 具体来说,在mount()返回调用之前,product.product_imagesis undefined,因为您product作为空对象启动。

Vue 2 风格的解决方案

  • 实例product.product_image化为可迭代:
//...
  data: () => ({ 
    product: { product_images: [] }
  })
Run Code Online (Sandbox Code Playgroud)

或在模板中提供一个空数组作为后备:

<ion-slide v-for="image in product.product_images || []" v-bind:key="image.id">
  <h1>Slide 1</h1>
</ion-slide> 
Run Code Online (Sandbox Code Playgroud)

或将 av-if放在 的父级上v-for

<ion-slides pager="true" :options="slideOpts" v-if="product.product_images">
  ...
</ion-slides>
Run Code Online (Sandbox Code Playgroud)

Vue 3 风格的解决方案

ProductDetails通过给它一个async setup函数,使整个组件悬而未决。在setup函数中,调用获取产品。
概念证明:

//...
async setup() {
  const product = await fetch("http://localhost:4000/api/products/" + 
    this.$route.params.id, {
      method: "get",
    }).then(r => r.json());
  return { product }
}
Run Code Online (Sandbox Code Playgroud)

现在<product-details>放入<Suspense>'s <template #default>,提供一个回退模板(将在<Suspense>解析其默认模板中找到的所有异步组件时呈现):

<Suspense>
  <template #default>
    <product-details></product-details>
  </template>
  <template #fallback>
    Product is loading...
  </template>
</Suspense>
Run Code Online (Sandbox Code Playgroud)

使用的美妙(和优雅)<Suspense>在于父级不需要知道标记尚未呈现的实际条件。它只是等待所有可疑组件解决。
简而言之,使用<Suspense>您不再需要使用v-ifs将渲染逻辑硬编码到模板中,并在包装​​器上明确指定条件。每个异步子组件都包含自己的条件,并且它向父组件宣布的所有内容是:我完成了。全部完成后,它们被渲染。

  • 一年过去了,悬念功能仍然被标记为实验性的。如果这个问题能够得到解决那就太好了,这样我们就可以在生产应用程序中使用它。 (4认同)