javascript - Dynamic import of routes into Vue router - Stack Overflow
I am creating a Vue starter kit app for a platform, and it will use a standard directory structure for creating all of the items needed for a resource (.vue
files, routes, Vuex store modules, etc). I would like to take advantage of this known structure to dynamically load router path objects so the user doesn't have to manually add routes to the router index file.
For example, here's a sample directory structure:
/src
|
---resources
|
-------user
|
---User.vue
---routes.js
---store.js
event
|
---Event.vue
---routes.js
---store.js
job
|
---Job.vue
---routes.js
---store.js
The inside of a routes.js
file looks like this:
import Event from '@/resources/event/Event'
export default [
{
path: '/events',
name: 'event',
ponent: Event
},
];
To do this manually in a standard router file (router.js
or router/index/js
), you would do something like this:
import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/ponents/Home'
import Auth from '@/ponents/Auth';
import eventRoutes from '@/resources/event/routes.js';
import userRoutes from '@/resources/user/routes.js';
import jobRoutes from '@/resources/job/routes.js';
Vue.use(Router);
let baseRoutes = [
{
path: '/',
name: 'home',
ponent: Home
},
{
path: '/login',
name: 'auth',
ponent: Auth
},
];
const routes = baseRoutes.concat(shiftCalendarRoutes)
.concat(eventRoutes)
.concat(userRoutes)
.concat(jobRoutes);
export default new Router({
mode: 'history',
routes,
})
What I would really like to do is this:
import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/ponents/Home'
import Auth from '@/ponents/Auth';
Vue.use(Router);
let routes = [
{
path: '/',
name: 'home',
ponent: Home
},
{
path: '/login',
name: 'auth',
ponent: Auth
},
];
function loadRoutes() {
const routes = require.context('@/resources', true, /routes.js$/i);
routes.keys().forEach((key) => {
const path = '@/resources/' + key.substring(2);
// Dynamically import file using import statement
import something from path;
// Add imported default object to routes object.
});
return routesList;
}
export default new Router({
mode: 'history',
routes,
})
The problem I'm running into is the actual import. When I try something like
routeItems.keys().forEach((key) => {
// routesList.push('@/resources/' + key.substring(2));
const path = '@/resources/' + key.substring(2);
const routeModule = import(path).default();
});
I get a Critical dependency: the request of a dependency is an expression
webpack error. I've tried other versions of import
without any luck.
Is it possible to do what I'm trying to do with dynamic importing?
I am creating a Vue starter kit app for a platform, and it will use a standard directory structure for creating all of the items needed for a resource (.vue
files, routes, Vuex store modules, etc). I would like to take advantage of this known structure to dynamically load router path objects so the user doesn't have to manually add routes to the router index file.
For example, here's a sample directory structure:
/src
|
---resources
|
-------user
|
---User.vue
---routes.js
---store.js
event
|
---Event.vue
---routes.js
---store.js
job
|
---Job.vue
---routes.js
---store.js
The inside of a routes.js
file looks like this:
import Event from '@/resources/event/Event'
export default [
{
path: '/events',
name: 'event',
ponent: Event
},
];
To do this manually in a standard router file (router.js
or router/index/js
), you would do something like this:
import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/ponents/Home'
import Auth from '@/ponents/Auth';
import eventRoutes from '@/resources/event/routes.js';
import userRoutes from '@/resources/user/routes.js';
import jobRoutes from '@/resources/job/routes.js';
Vue.use(Router);
let baseRoutes = [
{
path: '/',
name: 'home',
ponent: Home
},
{
path: '/login',
name: 'auth',
ponent: Auth
},
];
const routes = baseRoutes.concat(shiftCalendarRoutes)
.concat(eventRoutes)
.concat(userRoutes)
.concat(jobRoutes);
export default new Router({
mode: 'history',
routes,
})
What I would really like to do is this:
import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/ponents/Home'
import Auth from '@/ponents/Auth';
Vue.use(Router);
let routes = [
{
path: '/',
name: 'home',
ponent: Home
},
{
path: '/login',
name: 'auth',
ponent: Auth
},
];
function loadRoutes() {
const routes = require.context('@/resources', true, /routes.js$/i);
routes.keys().forEach((key) => {
const path = '@/resources/' + key.substring(2);
// Dynamically import file using import statement
import something from path;
// Add imported default object to routes object.
});
return routesList;
}
export default new Router({
mode: 'history',
routes,
})
The problem I'm running into is the actual import. When I try something like
routeItems.keys().forEach((key) => {
// routesList.push('@/resources/' + key.substring(2));
const path = '@/resources/' + key.substring(2);
const routeModule = import(path).default();
});
I get a Critical dependency: the request of a dependency is an expression
webpack error. I've tried other versions of import
without any luck.
Is it possible to do what I'm trying to do with dynamic importing?
Share Improve this question asked Apr 16, 2020 at 20:03 wonder95wonder95 4,3158 gold badges50 silver badges82 bronze badges1 Answer
Reset to default 4It doesn't really make sense to use import()
together with require.context()
, since the latter already provides the import. Given your use case, you only need require.context()
.
require.context()
returns a context module, which exports a keys
function that returns an array of all possible requests (i.e., the matching paths), each of which could be passed to the context itself (which invokes its resolve
function) to import the corresponding module:
function loadRoutes() {
const context = require.context('@/resources', true, /routes.js$/i)
return context.keys()
.map(context) // import module
.map(m => m.default) // get `default` export from each resolved module
}
- 2020年世界信息安全大会将于11月24日在蓉举办
- 外媒称谷歌正筹划自主开发安卓手机
- 手机防盗软件拍下“嫌疑人”可当呈堂证供
- [连载]巨头“心血之作”终失败(六):RIM PlayBook
- 有事请烧纸!清明祭奠已死的平板电脑
- 应用软件战场:安卓不敌苹果
- c# - Unity new input system generating multiple events - Stack Overflow
- R CatBoost support for incremental training - Stack Overflow
- excel - Challange with DSUM Function: How can I use the DSUM function to sum values based on criteria that match the starting ch
- uiviewcontroller - MacCatalyst Scene Frame needs adjustment - Stack Overflow
- node.js - Cors errors S3 Upload node - Stack Overflow
- javascript - Is there a way to get the difference between 2 points by click and hold with the mouse on a line on chart.js? - Sta
- google colaboratory - Load a Kaggle dataset into Colab notebook without extracting it - Stack Overflow
- powerbi - Keep Date Filter Active After Hiding Matrix-Based Calendar in Report Viewer - Stack Overflow
- javascript - Is there any way of getting an error message from the browsers XslProcessor object when using xsl:message terminate
- python - pickle port? loading a pickle with data when I've redefined the object code - Stack Overflow
- node.js - How to Deploy an Angular 19 SSR App on AWS Amplify? - Stack Overflow