组件在带有模块的NUXT 3中自动导入

发布于 2025-02-11 12:31:58 字数 1778 浏览 2 评论 0原文

我想与nuxt一起使用模块(因此不是默认结构目录),类似的东西:

“在此处输入图像描述”

我不知道如何配置nuxt或我的模块以自动导入组件嵌套文件夹,例如card文件夹。例如,planchoicecard.vue组件是自动导入的,但不是nestedcomponent.vue

这是我的nuxt.config.js

import { defineNuxtConfig } from "nuxt";

export default defineNuxtConfig({
  ssr: true,
  ignore: ["node_modules/**/*"],
  plugins: ["~/plugins/vue-i18n.client.ts"],
  runtimeConfig: {
    public: {
      myapp: {
        version: "1.7.8.2",
      },
    },
  },
  modules: ["./modules/Plans"],
  build: {
    postcss: {
      postcssOptions: {
        plugins: {
          tailwindcss: {},
          autoprefixer: {},
        },
      },
    },
  },
  css: ["~/assets/css/tailwind.css"],
});

和配置的配置 : 计划模块:

import path from "path";
import { defineNuxtModule } from "@nuxt/kit";

export default defineNuxtModule({
  // Default configuration options for your module
  defaults: {},
  hooks: {
    // ROUTES
    "pages:extend"(pages) {
      pages.push({
        name: "user",
        path: "/",
        file: path.resolve(__dirname, "./views/PlanChoices.vue"),
      });
    },

    // COMPONENTS
    "components:dirs"(dirs) {
      // Add ./components dir to the list
      dirs.push({
        path: path.resolve(__dirname, "./components"),
      });
    },

    // COMPOSABLES
    // "autoImports:dirs"(dirs) {
    //  dirs.push(path.resolve(__dirname, "./composables"));
    // },
  },
  async setup(moduleOptions, nuxt) {
    // -- Add your module logic here --
  },
});

I want to use modules (so not the default structure directories) with Nuxt, something like that:

enter image description here

The problem is that I don't know how to configure Nuxt or my module to auto import the components in nested folders like the Card folders. For example the PlanChoiceCard.vue component is auto imported but not NestedComponent.vue

This is my nuxt.config.js:

import { defineNuxtConfig } from "nuxt";

export default defineNuxtConfig({
  ssr: true,
  ignore: ["node_modules/**/*"],
  plugins: ["~/plugins/vue-i18n.client.ts"],
  runtimeConfig: {
    public: {
      myapp: {
        version: "1.7.8.2",
      },
    },
  },
  modules: ["./modules/Plans"],
  build: {
    postcss: {
      postcssOptions: {
        plugins: {
          tailwindcss: {},
          autoprefixer: {},
        },
      },
    },
  },
  css: ["~/assets/css/tailwind.css"],
});

and the configuration of the Plans module:

import path from "path";
import { defineNuxtModule } from "@nuxt/kit";

export default defineNuxtModule({
  // Default configuration options for your module
  defaults: {},
  hooks: {
    // ROUTES
    "pages:extend"(pages) {
      pages.push({
        name: "user",
        path: "/",
        file: path.resolve(__dirname, "./views/PlanChoices.vue"),
      });
    },

    // COMPONENTS
    "components:dirs"(dirs) {
      // Add ./components dir to the list
      dirs.push({
        path: path.resolve(__dirname, "./components"),
      });
    },

    // COMPOSABLES
    // "autoImports:dirs"(dirs) {
    //  dirs.push(path.resolve(__dirname, "./composables"));
    // },
  },
  async setup(moduleOptions, nuxt) {
    // -- Add your module logic here --
  },
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉 2025-02-18 12:31:58

答案是使与nuxt.config.ts中的相同:

from /a/66336654/10805872

so:so:

    // COMPONENTS
    "components:dirs"(dirs) {
      // Add ./components dir to the list
      dirs.push({
        path: path.resolve(__dirname, "./components"),
      });
    },

so: ste:

    // COMPONENTS
    "components:dirs"(dirs) {
      // Add ./components dir to the list
      dirs.push({
        path: path.resolve(__dirname, "./components"), pathPrefix: false
      });
    },

使用前缀,您必须使用它,例如模块/user/user/localeswitcher.vue,as &code>< user -locale-switcher/>

没有前缀so pathprefix:false,您可以直接使用< locale-switcher/>

The answer was to make the same as in nuxt.config.ts:

from https://stackoverflow.com/a/66336654/10805872

So:

    // COMPONENTS
    "components:dirs"(dirs) {
      // Add ./components dir to the list
      dirs.push({
        path: path.resolve(__dirname, "./components"),
      });
    },

become:

    // COMPONENTS
    "components:dirs"(dirs) {
      // Add ./components dir to the list
      dirs.push({
        path: path.resolve(__dirname, "./components"), pathPrefix: false
      });
    },

With the prefix you have to use it, for example a component in modules/user/localeSwitcher.vue, as <user-locale-switcher />

Without prefix so pathPrefix: false, you can use directly <locale-switcher />

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文