You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nuxt3项目中如何正确使用vue.draggable.next拖拽组件

报错原因

两个警告的根源有两点:

  1. 注册逻辑错误:vue.draggable.next 不是标准Vue插件,没有暴露install方法,无法通过app.use()注册;同时你使用了默认导入,拿到的导入值本身就不是有效组件/插件。
  2. 未做SSR兼容:Nuxt3默认开启服务端渲染,拖拽组件依赖浏览器DOM API,服务端执行时会出现异常,也会导致组件注册失败,最终触发组件解析失败的警告。
正确接入步骤

你可以根据项目使用场景,任选以下一种方案接入:

方案1:全局注册(适合多页面复用拖拽组件)

  1. 将你之前创建的插件文件重命名,必须带.client后缀,保证插件仅在客户端加载,比如命名为 plugins/vueDraggable.client.js
  2. 修改插件内的注册代码,不要用use注册插件,改为全局注册组件,注意使用具名导入:
import { VueDraggableNext } from "vue-draggable-next";

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.vueApp.component("VueDraggableNext", VueDraggableNext);
});
  1. 重启Nuxt开发服务,业务组件里无需再单独导入,直接使用<VueDraggableNext>标签即可。

方案2:单组件按需引入(适合仅个别页面使用)

不需要写全局插件,直接在使用拖拽的业务组件中具名导入组件即可,同时为了避免SSR报错,用Nuxt内置的<ClientOnly>组件包裹拖拽区域:

<template>
  <div class="h-full w-full border-2 border-dashed rounded-lg p-5 flex">
    <div class="flex w-1/6 h-full">
      <ComponentPalette />
    </div>
    <ClientOnly>
      <VueDraggableNext
        v-model="form.children"
        group="people"
        @start="isDragOver = true"
        @end="isDragOver = false"
        item-key="id"
        class="flex flex-col w-5/6 h-full border-blue-700 border-2 border-dashed rounded-lg p-5 space-y-5"
      >
        <template #item="{element}">
          <FormBuilder
            :component="element"
            @update="update"
          />
        </template>
      </VueDraggableNext>
    </ClientOnly>
  </div>
</template>

<script setup>
import FormBuilder from "~~/components/dynamic-components/FormBuilder.vue";
import ComponentPalette from "~~/components/form-builder/ComponentPalette.vue";
import { VueDraggableNext } from "vue-draggable-next";
import { v4 as uuidv4 } from "uuid";
const form = reactive({
  formId: "abcd-1234",
  formName: "User Registration",
  children: [],
});

const isDragOver = ref(false);
</script>
<style scoped></style>
关键注意点
  • 必须使用具名导入import { VueDraggableNext } from "vue-draggable-next",默认导入拿到的不是有效组件,这是触发插件格式错误警告的核心原因之一。
  • 必须做客户端渲染隔离:要么插件文件带.client后缀,要么用<ClientOnly>包裹组件,否则服务端渲染阶段会因为没有浏览器DOM API抛出异常。
  • 修改配置后记得重启Nuxt开发服务,避免缓存导致配置不生效。

内容的提问来源于stack exchange,提问作者Sathya Molagoda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 15:57:13