简体   繁体   English

Vue路由器无法延迟加载子组件

[英]Vue-router cannot lazy load child component

I'm using vue-router inside my project, which is working with a library that is, unfortunately, not in active development anymore. 我在项目内使用vue-router,不幸的是,该库正在与不在活动开发中的库一起使用。 I needed to add a few things there, so I downloaded the source, edited it myself and then built it into my project's "externals" folder. 我需要在其中添加一些内容,因此我下载了源代码,自己对其进行了编辑,然后将其构建到项目的“外部”文件夹中。

Now, until the project was not using front-end router everything worked fine, but lately, I implemented the vue-router with lazy-loading components and things got a bit trickier. 现在,直到该项目不使用前端路由器,一切都可以正常工作,但是最近,我用延迟加载组件实现了vue-router,事情变得有些棘手。

Library I was talking about is saved inside @/externals/date-picker . 我刚才说的库保存在@/externals/date-picker Inside my lazy-loaded component I import it using es6 imports: 在我的延迟加载组件内部,我使用es6 import导入它:

import DatePicker from '@/externals/date-picker';

Which triggers an error inside vue-router : 触发vue-router内部错误:

Failed to resolve async component default: TypeError: Cannot set property 'vue-date-picker' of undefined 无法解析异步组件默认值:TypeError:无法设置未定义的属性“ vue-date-picker”

I have no idea what's going on, but it seems like it cannot add the date-picker to module.exports . 我不知道发生了什么,但是似乎无法将date- module.exports添加到module.exports

I'm pretty sure there has to be the way to load external files even though they're lazy loaded. 我很确定即使是延迟加载的外部文件也必须有一种加载方法。 So, my question is, how can I add the external file/package to my lazy-loaded component? 因此,我的问题是,如何将外部文件/程序包添加到延迟加载的组件中? Thank you in advance. 先感谢您。

CODE

// router.js
import Vue from 'vue';
import Router from 'vue-router';
Vue.use(Router);

export default new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [
        {
            path: '/something',
            name: 'something',
            title: 'something',
            component: () => import(/* webpackChunkName: "something" */ './views/Component.vue')
        }
    ]
});

// Component.vue
<script>
    import DatePickerOriginal from "@/external/date-picker";

    export default {
        components: {
            DatePickerOriginal
        }
    };
</script>

And the source code of @/external/date-picker is almost the same as here (well, the build tooling was the same, and the application itself most likely does not cause the problem). @/external/date-picker的源代码与此处几乎相同(嗯,构建工具是相同的,并且应用程序本身很可能不会引起问题)。 Also, it might be nice to point out once again, the application is not in the node_modules folder with other packages, but is loaded from @/externals/date-picker . 另外,再次指出,应用程序不在其他软件包的node_modules文件夹中,而是从@/externals/date-picker node_modules加载的。

Looks like I was missing libraryTarget: 'window' in my webpack config, while merging the package in build process. 看起来像我在Webpack配置中缺少libraryTarget: 'window'时,在构建过程中合并包。

merge(commonConfig, {
    entry: path.resolve(__dirname + '/src/plugin.js'),
    output: {
        // ...
        libraryTarget: 'window'
    }
})

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

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