简体   繁体   English

如何在 Nuxt 3 项目中使用 vue3 组件(vue.draggable.next)作为插件

[英]How to use a vue3 component(vue.draggable.next) inside Nuxt 3 project as a plugin

In my recently started project, I wanted to use a vue.draggable.next .在我最近开始的项目中,我想使用vue.draggable.next So I created a ".js" file inside the nuxt plugin directory and I add code as below.所以我在 nuxt 插件目录中创建了一个“.js”文件,并添加了如下代码。

import VueDraggableNext from "vue-draggable-next";

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.vueApp.use(VueDraggableNext);
});

Then I used it in one of my components as below,然后我在我的一个组件中使用它,如下所示,

<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>
    <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>
  </div>
</template>

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

const isDragOver = ref(false);
</script>
<style scoped></style>

once I run the project I will get following errors:一旦我运行该项目,我将收到以下错误:

[Vue warn]: A plugin must either be a function or an obj
ect with an "install" function.
[Vue warn]: Failed to resolve component: VueDraggableNex
t
If this is a native custom element, make sure to exclude
 it from component resolution via compilerOptions.isCust
omElement.

How can I use this vue plugin properly in a nuxt3 project?如何在 nuxt3 项目中正确使用这个 vue 插件?

have some differences between a Vue Plugin and a Nuxt Plugin. Vue 插件和 Nuxt 插件之间存在一些差异。 What you are trying to do is create a Nuxt Plugin to use a Vue Component.您要做的是创建一个 Nuxt 插件来使用 Vue 组件。 So in order to do this, you need to update your code to:因此,为了做到这一点,您需要将代码更新为:

import { VueDraggableNext } from "vue-draggable-next";

export default defineNuxtPlugin((nuxtApp) => {
  nuxtApp.vueApp.component("draggable", VueDraggableNext);
});

The difference is the way you are registering the component in vueApp.不同之处在于您在 vueApp 中注册组件的方式。 Also with this change, you will need to update the component name inside the html template to <draggable>同样通过此更改,您需要将 html 模板中的组件名称更新为<draggable>

Here follow some useful links if you want to know more:如果您想了解更多信息,请点击一些有用的链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM