简体   繁体   English

延迟在ui-router中将Angular指令作为Webpack块加载

[英]Lazy loading Angular directives in ui-router as Webpack chunks

I am trying to lazy load an Angular directive as a webpack chunk. 我试图延迟加载Angular指令作为webpack块。

Here is my current config attempt at using ocLazyLoad : 这是我当前使用ocLazyLoad的配置尝试:

// Basic Config
function routingBase( $urlRouterProvider, $locationProvider, $stateProvider ) { 

    $locationProvider.html5Mode(true);

    $stateProvider
        .state('home', {
            url: '/home',
            template: '<app-main></app-main>',
            resolve: {
                load: ( $q, $ocLazyLoad ) => {

                    let deferred = $q.defer(); 

                    require.ensure([], (require) => {

                        // Load entire module
                        let module = require('../modules/main');

                        $ocLazyLoad.load({ 
                            name: module.name 
                        });

                        deferred.resolve(module);

                    }, 'app-main');

                    return deferred.promise;
                }
            }
        })
}

This goes in myModule.config(routingBase); 这进入myModule.config(routingBase); .

../modules/main is just an angular module that exports a directive (eg export default angular.module('main',[]).directive('appMain', appMainFn); . ../modules/main只是一个导出指令的角度模块(例如, export default angular.module('main',[]).directive('appMain', appMainFn);

Any tips? 有小费吗? What I am getting is that the <app-main></app-main> is correctly added to the document, and that the chunk is correctly loaded as module. 我得到的是<app-main></app-main>已正确添加到文档中,并且该块已正确加载为模块。 But it is not replaced (it stays as <app-main></app-main> ). 但是它不会被替换(它保持为<app-main></app-main> )。

Would you recommend a different method for lazy loading chunks (maybe using $compileProvider )? 您是否会推荐一种不同的方法来延迟加载块(也许使用$compileProvider )? I would like the cleanest possible way. 我想要最干净的方法。

Thank you very much for your help. 非常感谢您的帮助。

I was able to get this working in my current project. 我能够在当前项目中使用它。 Just change the following line and see it works. 只需更改以下行即可看到它的工作原理。 You need to add default as well since you are exporting angular module as default. 您还需要添加默认值 ,因为您要将角度模块导出为默认值。 Cheers! 干杯!

$ocLazyLoad.load({ 
name: module.default.name 
});

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

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