【发布时间】:2019-09-15 08:42:09
【问题描述】:
我在 nuxt.js 中有一个 components 文件夹
/components/atoms/
在该文件夹中,我有一个 index.js 来动态导出所有组件
const req = require.context('./', true, /\.vue$/)
const components = {}
req.keys().forEach(fileName => {
const componentName = fileName.replace(/^.+\/([^/]+)\.vue/, '$1')
components[componentName] = req(fileName).default
})
export const { ButtonStyled, TextLead, InputSearch } = components
所以我可以随心所欲地完美导入
import { ButtonStyled } from "@/components/atoms"
问题是我正在定义要静态导出的变量,固定的,所以对于每个创建的组件,我需要手动添加另一个变量
我需要动态导出变量名
例子:
DynamicCreation = ['ButtonStyled', 'TextLead', 'InputSearch']
export const { DynamicCreation } = components
// output -> export const { ButtonStyled, TextLead,InputSearch } = components
我需要导出已经是非结构化变量的名称
注意:我不能使用这个export default components,因为我不能像这样导入import { ButtonStyled } from "@/components/atoms"
【问题讨论】:
标签: javascript node.js vue.js nuxt.js atomic-design