1

I have a components folder in nuxt.js

/components/atoms/

and inside that folder I have an index.js to export all components dynamically

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

so I can import perfectly as I wish

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

the problem is that I am defining the variables to be exported statically, fixed, so for each created component I would need to add another variable manually

I need to dynamically export the variable name

Example:

DynamicCreation = ['ButtonStyled', 'TextLead', 'InputSearch']

export const { DynamicCreation } = components 

// output -> export const { ButtonStyled, TextLead,InputSearch  } = components

I need to export the name of already unstructured variables

Note: I can not use this export default components because I can not import like this import { ButtonStyled } from "@/components/atoms"

Yung Silva
  • 1,324
  • 4
  • 20
  • 40

3 Answers3

0

You should be able to do it like this:

export default components

Then in your file where you want to use the components:

import * as components from '@/components/atoms'

Then when you need to use the components in your Vue files, you need to map them:

@Component({
  components: {
    ButtonStyled: components.ButtonStyled
  }
})

And now you have:

<ButtonStyled></ButtonStyled>
Ohgodwhy
  • 49,779
  • 11
  • 80
  • 110
0

You can make something like this way, check if is what do you need.

Create a file to import a conjunct of components: allComponents.js

export default {
 componentOne: require('./passToOneComponent.js');
 componentTwo: require('./passToOneComponent.js');
 componentThree: require('./passToOneComponent.js');
}

After in index.js export the allComponents.js with the name that you wish:

export {default as SomeName } from 'allComponents.js';

So in the final file, you can make something like:

import { SomeName } from 'index.js';

SomeName.componentOne();
0

I created a library that does this type of export, anyone who wants can install via npm

I created a Webpack Plugin that makes named exports from a component, maybe this helps other people

Weback Plugin - named-exports

Community
  • 1
  • 1
Yung Silva
  • 1,324
  • 4
  • 20
  • 40