简体   繁体   English

尝试在 Vue 中延迟加载组件文件时找不到模块

[英]Module not found when trying to lazy load component file in Vue

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错误:在 eval 中找不到模块“@/components/pages/UsersPage.vue”(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:这是名为 Multiwindows 的组件(简化版),我尝试在其中加载所选组件:

<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. window是一个作为 prop 传递的对象,包括所选组件所需的数据。 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多窗口.vue
      • pages
        • UsersPage.vue用户页面.vue

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:我无法异步导入UsersPage.vue ,如果我将路径常量更改为相对路径也没关系,因为我是从 MultiWindows 导入的,应该是这样的:

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.js?9be2:1687 WebSocket 连接到“wss://mydomain/sockjs-node/919/1qzk04x2/websocket”失败:

sockjs.bundle.js:1 Uncaught Error: Incompatible SockJS! sockjs.bundle.js:1 未捕获的错误:SockJS 不兼容! Main site uses: "1.5.2", the iframe: "1.5.0".主站点使用:“1.5.2”,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.我使用 NPM 明确安装了 sockjs-client 包,以防它是由于版本过时。 No success.没有成功。

I tested a solution for using dynamic component and lazy load them in Vue-js version 2 .我测试了一个使用动态组件的解决方案,并在 Vue-js 版本2 中延迟加载它们。 I'm not sure that it works for version 3 .我不确定它是否适用于 version 3 I used computed properties instead of Vue methods.我使用计算属性而不是 Vue 方法。 Here is my index page code that passes the data to containerCompo component (the corresponding Multiwindows component in the question):这是我将数据传递给containerCompo组件(问题中相应的 Multiwindows 组件)的索引页代码:

 <template> <div class="about"> <v-container class="px-0" fluid > <v-row> <v-col md="12"> <container-compo :window1="compoData"> </container-compo> </v-col> </v-row> </v-container> </div> </template> <script> import containerCompo from "../components/containerCompo"; export default { data () { return { compoData: { name: 'Users', class: 'far fa-user', component1: 'dynamic1', // the name of dynamic component } } }, components: { containerCompo, } } </script> <style scoped> </style>

the containerCompo has "window1" prop that accepts the data include the component name. containerCompo 有“window1”道具,它接受包括组件名称在内的数据。 Here is the code of containerCompo component:这是containerCompo组件的代码:

 <template> <section> <component v-if="isCompoVisible" :is="getComponent"></component> <v-btn @click="isCompoVisible= true">click me</v-btn> </section> </template> <script> export default { name: 'containerCompo', data () { return { isCompoVisible: false, } }, props: ["window1"], computed: { getComponent() { let data = this.window1.component1; return () => import(`./${data}`); } } } </script>

The getComponent computed data processes the window1 prop and extract the dynamic component name to be used in vue <component> tag. getComponent计算数据处理window1 属性并提取要在 vue <component>标签中使用的动态组件名称。 I supposed that containerCompo.vue and all dynamic components are in the same level in components folder of Vue and index.vue is in the views folder.我假设containerCompo.vue和所有动态组件在 Vue 的components文件夹中处于同一级别,而index.vueviews文件夹中。

This is not the complete solution but it may help you这不是完整的解决方案,但它可以帮助您

You need to register a component before using it and there is also a method to dynamically import components defineAsyncComponent使用前需要注册一个组件,还有一个方法可以动态导入组件defineAsyncComponent

import app from '@/main';
import { defineAsyncComponent } from 'vue';
....
 // Check if the component has already been registered.
 if (!app._context.components[componentName]) {
    app.component(
        componentName,
        defineAsyncComponent(() => import(`@/components/pages/${componentName}.vue`)),
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM