1

is there a way to build all .vue files in my views/ folder without importing them into my vue application directly? What I want to achieve is a Router with dynamic route definitions which will be filled from a configuration which is a json filled with name, router path and the component name which should be lazy loaded. My issue right now: The application does only build the files which are imported somewhere in my application. What I need: Build all files in views/ as chunk in order to be able to have a dynamic Router definition.

Thank you for your help :)

1

2 Answers 2

2

It's hard to give proper solution without any code provided, but I'll try

With code like this:

// route definition loaded from json const routeDef = [ { name: 'about', path: '/about', componentName: 'about' }, { name: 'profile', path: '/profile', componentName: 'profile' } ] // we can create routes like this const routes = routeDef.map(function(def) { return { name: def.name, path: def.path, component: () => import('@/views/' + def.componentName + '.vue') } }) const router = new VueRouter({ routes: routes }) 

By using import('@/views/' + def.componentName + '.vue') you are telling Webpack I want to import one of the .vue files in the views directory but which one will be known at runtime. So Webpack will bundle all file in that directory and make it available for import at runtime....

Note that what you pass into import() is really important - part of the argument must always be a static string so Webpack have at least some clue about the directory (import(def.componentName) wont work) - see the docs

Additional example - loading route definitions from server at runtime

router.js

export default createRouter() { return axios.get('/getRouteDefinitions').then(function(data) { const routes = data.routeDef.map(function(def) { return { name: def.name, path: def.path, component: () => import('@/views/' + def.componentName + '.vue') } }) return new VueRouter({ routes: routes }) }) } 

main.js

import VueRouter from `VueRouter` import createRouter from `router.js` Vue.use(VueRouter) createRouter().then(function(router) { const app = new Vue({ router }) app.$mount("#app") }) 
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your answer. So I have to define the list before building the frontend? There is no way to say "okay package X is building some views seperatly and package Y is getting some async route definitions with the named views from package X"
So I have to define the list before building the frontend What list? You mean list of routes (reouteDef in my example) ? No, you dont - that list can be loaded asynchronously from the server at runtime. But of course code will look a bit different because you need to create Router after the data are loaded...
Okay thanks. The way with import to lazy load the vie of a route is something I knew already. But do you know how I can tell webpack via vue.config.js to compile all file in /views? Because on build the application doesn't know anthing about the routes because they are loading while executing the webpage so it doesn't compile my views/*.vue files at all. So the application can't find the view defined fore the route in my router.
Maybe there is some other problem with your own code (that's why its always good to post some code) but the code in my answer is enough to make Webpack bundle all vue files from @/views directory. It doesn't need to know anything about your routes, that one import() statement says it all....
Okay I will try this. The problem is: At the moment I just try and error stuff for my "concept" :D so there is no real code yet. I'm trying stuff, removing it again, starting over ^^ Just have basic home and home2 vue with hello world on it and a router so nothing fancy. Basic setup from vue-cli create process
|
0

The unplugin-vue-components plugin can automatically import components that are used in your application without needing to specifically import ... each one. It also supports async components which are "lazy loaded". This sounds like what you're after.

I haven't had a chance to put together a code sample here, but if anyone has one, feel free to edit this answer to include one.

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.