【问题标题】:How can I dynamically export components in index.js?如何在 index.js 中动态导出组件?
【发布时间】:2019-09-14 23:32:32
【问题描述】:

我正在使用 nuxt.js 开发一个项目,我想实现原子设计方法

所以我目前导入这样的组件

import ButtonStyled from '@/components/atoms/ButtonStyled.vue'
import TextLead from '@/components/atoms/TextLead.vue'
import InputSearch from '@/components/atoms/InputSearch.vue'

但我需要像这样导入

import {
    ButtonStyled,
    TextLead,
    InputSearch
} from '@/components/atoms'

我离得越近,

/atoms/index.js
const req = require.context('./', true, /\.vue$/)

const modules = {}

req.keys().forEach(fileName => {
  const componentName = fileName.replace(/^.+\/([^/]+)\.vue/, '$1')
  modules[componentName] = req(fileName).default
})

export const { ButtonStyled, TextLead } = modules

但我还是静态定义导出变量名,我需要根据文件夹内的组件来定义动态

注意:我不能使用

export default modules

如果我使用上面的代码sn-p我将无法导入我需要的方式,即:

import { ButtonStyled } from "@/components/atoms"

【问题讨论】:

    标签: javascript node.js vue.js nuxt.js atomic-design


    【解决方案1】:

    我创建了一个库来为我完成这一切,也许它可以帮助其他人。

    named-exports

    【讨论】:

      【解决方案2】:

      我尝试使用您的方式来做到这一点,并且知道您在 module.exports 处犯了一个错误
      module.exports 不能使用 import ,我想你可以这样做
      在atoms/index.js

      const req = require.context("./", true, /\.vue$/);
      const atoms = {};
      req.keys().forEach(fileName => {
        const componentName = fileName.replace(/^.+\/([^/]+)\.vue/, "$1");
        atoms[componentName] = req(fileName).default;
      });
      export default atoms;
      

      在哪里使用

      import k from "@/components/atoms/index.js";
      export default {
        components: {
          test1: k.test1,
          test2: k.test2
        }
      };
      

      或 index.js

      import test1 from "./test1.vue";
      import test2 from "./test2.vue";
      
      export { test1, test2 };
      

      以及像这样在哪里使用

      import {test1,test2} from "@/components/atoms/index.js";
      
      export default {
        components: {
          test1,
          test2
        }
      };
      

      【讨论】:

      • 仍然变得非常冗长,大,组件对象,我需要像我的问题中的解决方案```组件:{ ButtonStyled:atoms.ButtonStyled,TextLead:atoms.TextLead,InputSearch:atoms.InputSearch } ```
      • 或者你可以在你的 index.js :: import test1 from "./test1.vue" 中使用它;从“./test2.vue”导入 test2;导出 { test1, test2 };
      • 我已经把新代码放在了答案中,我无法自动导出目录下的所有文件
      • 所以不是动态导出,是静态的,固定的
      • 我知道你想要什么,但我认为没办法,exports不需要动态名称,exports default只能导入所有对象;我在 NPM 上看到了一些实用程序,他们使用静态方式来做到这一点。您可能需要在 MDN 上查看有关 export 的文章
      【解决方案3】:

      require.context 在 Webpack 中是一个非常晦涩的函数,在运行单元测试时你会遇到问题。但是,要解决您的问题;您需要在项目的main.js 中导入 index.js 文件。

      这就是我的做法:

      _globals.js

      // Globally register all base components prefixed with _base for convenience, because they
      // will be used very frequently. Components are registered using the
      // PascalCased version of their file name.
      import Vue from 'vue'
      import upperFirst from 'lodash/upperFirst'
      import camelCase from 'lodash/camelCase'
      
      const requireComponent = require.context('.', true, /_base-[\w-]+\.vue$/)
      
      requireComponent.keys().forEach(fileName => {
        const componentConfig = requireComponent(fileName)
      
        const componentName = upperFirst(
          camelCase(fileName.replace(/^\.\/_base/, '').replace(/\.\w+$/, ''))
        )
      
        Vue.component(componentName, componentConfig.default || componentConfig)
      })
      

      组件/index.js

      //...
      import './_globals'
      //...
      

      main.js

      //...
      import './components' // This imports in the index.js
      //...
      

      这样,您使用 require.context() 加载的组件将注册为 vue 组件并在全球范围内可用。我建议仅将全局组件与将大量使用的组件一起使用。如果您打算只使用一次,请不要全局加载组件。

      您可以在这里找到一个工作示例 -> https://github.com/IlyasDeckers/vuetiful/tree/master/src

      要让您的单元测试与 jest 一起工作,您需要模拟 require.context()。这是一个真正的痛苦,但可以通过使用 babel-plugin-transform-require-context 轻松实现

      【讨论】:

      • 问题描述中提到,我用的是nuxt.js,所以我没有main.js文件,也不想全局注册组件,这个跟没关系我的问题。我需要动态导出 index.js 中的组件,导出不同于全局注册,抱歉我的英语不好
      • 我更新了我的问题,尽可能接近解决方案,请看一下,如果我能提供帮助
      猜你喜欢
      • 2022-11-29
      • 2021-09-19
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      相关资源
      最近更新 更多