如何从 node_modules 延迟加载外部模块? [英] How do I lazy load an external module from node_modules?

查看:52
本文介绍了如何从 node_modules 延迟加载外部模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以像这个模块一样延迟加载本地模块

We can lazy load a local module like this module

    {
        path: 'somePpath',
        loadChildren: 'app/path/some.module#SomeModule'
    },

我们如何延迟加载来自外部库的模块驻留在 node_modules 中?

How do we lazy load a module that comes from an external library resides in node_modules?

推荐答案

要在路由器模块中加载外部模块,您可能需要使用 Wrapper 模块.在您拥有路由模块的同一个本地项目中创建一个包装器模块.使用传统的导入语法在此包装模块中导入您的外部模块.

To load an external module in the router module you might need to use a Wrapper module. Create a wrapper module in the same local project in which you are having your routing module. Import your external module in this wrapper module with traditional import syntax.

import { SomeModule } from '@externalLib';

将此模块包含在NgModule 的导入数组的导入中.

Include this module in the imports of NgModule's import array.

@NgModule({
  imports: [SomeModule]
})
export class SomeWrapperModule {
}

然后在路由器模块中使用这个包装模块,就像我们通常使用的模块一样.

Then use this wrapper module in the router module as we usually use a module.

// for Angular 7 and below 
{
    path: 'some-path',
    loadChildren: '../somewrapper.module#SomeWrapperModule'
}

// for Angular 8+
{
    path: 'some-path',
    loadChildren: () => import('../somewrapper.module').then(mod => mod.SomeWrapperModule)
}

这篇关于如何从 node_modules 延迟加载外部模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆