Vue (nuxt) 中的全局组件

Global components in Vue (nuxt)

在构建 Vue 应用程序时,我们在每个模板中重复使用某些 Vue 组件。我们的网格系统存在于 .region、.layout、.grid、.column 元素之外。它们都是独立的 Vue 组件 (, ...)。

我们现在在每个模板中都这样做:

import BlMain from '~components/frame/main/Main.vue'
import BlRegion from '~components/frame/region/Region.vue'
import BlLayout from '~components/frame/layout/Layout.vue'
import BlGrid from '~components/frame/grid/Grid.vue'
import BlColumn from '~components/frame/column/Column.vue'

有没有办法在你的项目中全局导入Vue组件? 是否可以选择创建一个包含上述导入的组件 Frame.vue 并在每个模板中添加 Frame 组件? 其他 FE 框架如何解决这个问题?

我们在 Vue 上使用 Nuxt JS

在文件夹布局下创建一个frame.vue,将所有组件导入其中并使其成为所有模板的布局,例如 /template.vue

你应该使用注册账户的插件。

// plugins/bl-components.js

import Vue from 'vue'
import BlMain from '~components/frame/main/Main.vue'
import BlRegion from '~components/frame/region/Region.vue'
import BlLayout from '~components/frame/layout/Layout.vue'
import BlGrid from '~components/frame/grid/Grid.vue'
import BlColumn from '~components/frame/column/Column.vue'
    
const components = { BlMain, BlRegion, ... }
   
Object.entries(components).forEach(([name, component]) => {
    Vue.component(name, component)
})
// nuxt.config.js

export default {
    plugins: ['~plugins/bl-components']
}

!!!始终以 Base 开头命名您的组件,例如:BaseIcon.vue
1.首先,你需要在你的插件文件夹中创建一个插件,我叫我的global.js
2. 安装lodash:npm install lodash
3. 在 global.js 内添加此代码:

import Vue from 'vue'
import upperFirst from 'lodash/upperFirst'
import camelCase from 'lodash/camelCase'

const requireComponent = require.context(
  '~/components',
  false,
  /Base[A-Z]\w+\.(vue|js)$/
)

requireComponent.keys().forEach((fileName) => {
  const componentConfig = requireComponent(fileName)

  const componentName = upperFirst(
    camelCase(fileName.replace(/^\.\/(.*)\.\w+$/, ''))
  )

  Vue.component(componentName, componentConfig.default || componentConfig)
})

  1. 在您的 nuxt.config.js 中添加 plugins: ['@plugins/global.js']
  2. 现在您只需键入 <BaseIcon />
  3. 即可使用您的基本组件

在您的 nuxt.config.js 中,将 components 设置为 true

https://nuxtjs.org/docs/2.x/configuration-glossary/configuration-components

您可以通过创建自定义插件来做到这一点 像这样..

import Vue from 'vue'

/** form */
import Email from '~/components/elements/form/Email'
import Mobile from '~/components/elements/form/Mobile.vue'
import Password from '~/components/elements/form/Password'
import TextInput from '~/components/elements/form/TextInput.vue'
import FormLayout from '~/components/elements/form/FormLayout.vue'
import SelectInput from '~/components/elements/form/SelectInput.vue'
import ConfirmPassword from '~/components/elements/form/ConfirmPassword'

/** snackbar */
import Snackbar from '~/components/elements/Snackbar.vue'

/** skeleton */
import PageListing from '~/components/skeleton/PageListing'

/** slots */
import OneRow from '~/components/slots/layouts/OneRow'
import LoginWrapper from '~/components/slots/layouts/LoginWrapper'

/** slots tab */
import TabHolder from '~/components/slots/layouts/TabHolder'
import TabHolderHeading from '~/components/slots/layouts/TabHolderHeading.vue'

/** gallery */
import GalleryInput from '~/components/gallery/GalleryInput.vue'
import GalleryDialog from '~/components/gallery/GalleryDialog.vue'

const components = { TabHolderHeading, TabHolder, GalleryInput, GalleryDialog, Snackbar, LoginWrapper, PageListing, OneRow, Password, FormLayout, ConfirmPassword, Email, Mobile, TextInput, SelectInput }

Object.entries(components).forEach(([name, component]) => {
  Vue.component(name, component)
})

现在通过在 nuxt.config.js

中调用它使其在全球范围内可用
plugins: [
    { src: '~/plugins/import-design-elements' }
],