12

I hate repeating things in code. For now I am importing vue files like this in my main.js.

import Default from '../../src/components/default.vue'; import Home from '../../src/components/home.vue'; import hakkinda from '../../src/components/hakkinda.vue'; import projeler from '../../src/components/projeler.vue'; import servisler from '../../src/components/servisler.vue'; import yetenekler from '../../src/components/yetenekler.vue'; import yetenek from '../../src/components/yetenek.vue'; import referanslar from '../../src/components/referanslar.vue'; import iletisim from '../../src/components/iletisim.vue'; 

Is there a way to do same thing with less lines? Could be great if I can assign variable name from filename. Can PHP help about it? But then how to compile main.js? I did not figured out.

3
  • are you using Webpack ? Commented Feb 13, 2017 at 9:05
  • Nope, I am using vue-cli to compile. But I think I can assign a config file when compiling. Is it help? Commented Feb 13, 2017 at 9:08
  • 1
    Vue CLI use the webpack. Commented Feb 13, 2017 at 9:59

4 Answers 4

10

I use this script in a file named "index.js" to "export default all exported default in every file in the current folder" sort of thing:

const files = require.context('.', false, /\.js$/) const modules = {} files.keys().forEach((key) => { if (key === './index.js') return modules[ key.replace(/(\.\/|\.js)/g, '') ] = files(key).default }) export default modules 

Then you can import the whole directory by importing it's name, just like this:

import folder from '../path/to/folder' 

I hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

I had to replace few things to get it to work. files(key).default with files(key). require.context('.', false, /\.js$/) with require.context('.', false, /\.vue$/). key.replace(/(\.\/|\.vue)/g, '') with key.replace(/(\.\/|\.vue)/g, ''). Thank you.
3

Based on Potray and Fosuze's working answers for easy reference:

const files = require.context('.', false, /\.vue$/) const modules = {} files.keys().forEach((key) => { if (key === './index.js') return modules[key.replace(/(\.\/|\.vue)/g, '')] = files(key) }) export default modules 

Comments

3

See the medium post I created

I'll explain it in an in-depth / easy way there.

For a quick answer here.

  • Create a folder (let's say colors folders for example)

  • create and or add your Vue files inside of that folder (sample red.vue, blue.vue, green.vue)

  • add to that folder (colors folder) an index.js containing (This is based on oprogfrogo's answer.).

const files = require.context('.', false, /\.vue$/) const modules = {} files.keys().forEach((key) => { if (key === './index.js') return modules[key.replace(/(\.\/|\.vue)/g, '')] = files(key) }) export default modules 

your directory looks like for example

  • colors/
  • colors/red.vue
  • colors/blue.vue
  • colors/green.vue
  • colors/index.js

then in any component, you can

 import colors from "./colors/index" //location of the folder console.log(colors); let yourComponent = colors['blue'].default // wil get you that component 

where ['is the file name']

then in your template, you can

<component v-bind:is="yourComponent"></component> 

2 Comments

Please remember to disclose that the blog post you are linking to is your own (source). Failure to disclose affiliations can result in your posts being treated as spam, potentially resulting in a lost of reputation or a suspension.
@JeremyCaney I now disclosed that it's made by me. Someone has to give an answer to this question. Thanks for your input.
1

unplugin-vue-components

This is a Vue plugin that can auto-import components when they are used. Supports both Vue 2 and Vue 3. See installation instructions here.

It will automatically turn this:

<template> <div> <HelloWorld msg="Hello Vue 3.0 + Vite" /> </div> </template> <script> export default { name: 'App', } </script> 

into this:

<template> <div> <HelloWorld msg="Hello Vue 3.0 + Vite" /> </div> </template> <script> import HelloWorld from './src/components/HelloWorld.vue' export default { name: 'App', components: { HelloWorld, }, } </script> 

Notice that it automatically does the import and also the components: {...} registration, so you don't have to.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.