从 Nuxt 2.15.0 开始,components
他们的工作方式发生了变化,如本文所述github问题 https://github.com/nuxt/nuxt.js/issues/8851#issuecomment-781351562.
根据您的结构以及您希望如何处理组织,您可以根据此处提供的迁移指南编辑您的配置:https://github.com/nuxt/components#v1-to-v2 https://github.com/nuxt/components#v1-to-v2
或者您也可以简单地设置pathPrefix
选项让您的所有组件都可用,根本没有任何前缀。
nuxt.config.js/ts
components: [
{
path: '~/components', // will get any components nested in let's say /components/test too
pathPrefix: false,
},
]
PS:该解决方案也适用于 Nuxt3。
该文档实际上确实需要更新:https://nuxtjs.org/docs/2.x/directory-struct/components#components-discovery https://nuxtjs.org/docs/2.x/directory-structure/components#components-discovery
它是这样工作的:对于给定的页面
<template>
<div>
<yolo-swag /> <!-- no need for <nested-yolo-swag /> here -->
</div>
</template>
并使用以下文件树
Nuxt3 更新
好像 http://v3.nuxtjs.org/guide/directory-structure/components/#dynamic-components这是新的官方语法
import { defineNuxtConfig } from 'nuxt'
export default defineNuxtConfig({
components: {
global: true,
dirs: ['~/components']
},
})