Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to import *.vue files in a folder?

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.

like image 271
fozuse Avatar asked Feb 13 '17 08:02

fozuse


People also ask

What are .VUE files?

VUE file extension is mainly associated with single file components used by Vue, an open-source, frontend JavaScript framework for building user interfaces. Such VUE single-file components are modules containing source code (typically for a single UI component) which can be exported or reused in a Vue application.

How do I import files into Vue js?

There are two ways to import a JavaScript library to the Vue Component. The first is to import a local JavaScript library. Here, you can import the JavaScript library by using the 'import' keyword inside the script tag of your Vue file. import * as mykey from '../assets/js/mykey.

What is VUE file format?

A VUE (. vue) file is an editable ASCII file. You create a VUE file using the VUE file renderer. A VUE file contains a sequence of frames to render.


1 Answers

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.

like image 154
Potray Avatar answered Sep 26 '22 17:09

Potray