xon52 / module-example-part-1

9 stars 7 forks source link

instead of loading routes from a file why not read all pages from pages folder ? #1

Open ramstein74 opened 6 years ago

ramstein74 commented 6 years ago

Hello im creating an app but i think its too tedious every time i create a page.vue i have to go to this file and add it manually.

import MyNewPage from '../pages/MyNewPage.vue'

export default [ { path: '/mynewpage', component: MyNewPage },

]

Your app already does some automatic loading but i think this will be an must for large apps . how can i do it programatically ? Thank you

xon52 commented 6 years ago

Apologies, I didn't see this until right now. I know what you are trying to do and I do not have a solution.

The problem is: for your code to do anything it must be compiled, and once it's compiled, it cannot gather files and re-compile itself. I think that to do what you want, you'd have to exclude all of those files, and then have your code fetch them and dynamically include them - which is WAY harder than just listing them.

I did want to do this dynamically myself, but ultimately, you must import or require any file you want to include somewhere.

** Having said that - you can use require() with variables. This means you could do what you want by either: Keeping a list of files in-code that you can loop through with require, or have a standardized file name system like Page1, Page2, Page3. Bad practice but you could then wrap that in a try-catch so you don't have to know exactly how many there are.

Ultimately, I don't think it's worth it, and I recommend manually keeping track of files for debugging purposes.