I'm trying to implement a way to display components on demand by means of dynamic components and lazy load.
However when it comes to dynamically importing the component file, I get this error:
Error: Cannot find module '@/components/pages/UsersPage.vue' at eval (eval at ./src/components/multiwindows lazy recursive
This is the component called Multiwindows (a cut short version), in which I try to load the selected component:
<template>
<component :is="getComponent()"></component>
</template>
<script>
export default {
props: ['window'],
methods: {
getComponent(window) {
const path = `@/components/pages/${this.window.component}.vue`;
return import(path);
}
}
}
</script>
window is an object passed as a prop including the data needed for the selected component. An example of it would be (note the component property which is the one taking place in the code above):
{
name: 'Users',
class: 'far fa-user',
component: 'UsersPage',
}
The file structure of my project is like this:
- src
- components
- multiwindows
- MultiWindows.vue
- pages
- UsersPage.vue
- multiwindows
- components
I can't get to asynchronously import UsersPage.vue, and it doesn't matter if I change the path constant to a relative path, which since I'm importing from MultiWindows, should be like this:
const path = `../pages/${window.component}.vue`;
or if I try with an absolute path, both starting with / or not:
const path = `/src/components/pages/${window.component}.vue`;
EDIT:
I also noticed these errors in console:
sockjs.js?9be2:1687 WebSocket connection to 'wss://mydomain/sockjs-node/919/1qzk04x2/websocket' failed:
sockjs.bundle.js:1 Uncaught Error: Incompatible SockJS! Main site uses: "1.5.2", the iframe: "1.5.0".
Chances are these errors are related to the module not being imported, but I don't know how to fix them. I explicitly installed the sockjs-client package with NPM in case it was due to an outdated version. No success.
EDIT 2:
Adapting my code to what @jeremy-castelli suggested:
async getComponent(window) {
const path = `@/components/pages/${this.window.component}.vue`;
return await defineAsyncComponent(() => import(path));
}
I got no error now, but instead I get this warning, and the component is still not displayed:
Component is missing template or render function.
The loaded component has all the elements: template, script, etc. I checked defineAsyncComponent returns an AsyncComponentWrapper, which is in turn assigned to the :is attribute of the component