我正在研究vue / 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'
解决方案?
index.js 在以下文件夹中 atoms
它运行完美(ES5)
// ES5 works ?
const req = require.context('.', false, /\.vue$/)
req.keys().forEach(fileName => {
const componentName = fileName.replace(/^.+\/([^/]+)\.vue/, '$1')
module.exports[componentName] = req(fileName).default
})
// ES6 does not work ?
// ERROR: Module build failed, 'import' and 'export' may only appear at the top level
const req = require.context('.', false, /\.vue$/)
req.keys().forEach(fileName => {
const componentName = fileName.replace(/^.+\/([^/]+)\.vue/, '$1')
export const [componentName] = req(fileName).default
})
nuxt use ES6
NOTE:我无法导出对象,因为无法使用,import {ButtonStyled}否则导入后必须解构对象
我需要导出以便可以使用
import { ButtonStyled } from '@/components/atoms'
我需要导出文件夹中每个组件的名称
任何建议,信息或建议,将不胜感激。
忽然笑
慕无忌1623718
相关分类