V
V
Vyacheslav2016-06-30 10:35:09
JavaScript
Vyacheslav, 2016-06-30 10:35:09

How to implement lazy loading of components in Angular 2?

How to implement lazy loading of components in Angular 2 (specifically - in the RC version)?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Kano, 2016-07-07
@Kano

Because at the moment, creating a component from a string is not yet implemented

function resolveComponent(resolver, snapshot) {
        // TODO: vsavkin change to typeof snapshot.component === 'string' in beta2
        if (snapshot.component && snapshot._routeConfig) {
            return resolver.resolveComponent(snapshot.component);
        }
        else {
            return Promise.resolve(null);
        }
    }

RuntimeCompiler.prototype.resolveComponent = function (component) {
            if (isString(component)) {
                return PromiseWrapper.reject(new BaseException$1("Cannot resolve component using '" + component + "'."), null);
            }
            return this.compileComponentAsync(component);
        };

Therefore, there is only one option for lazy loading, which is to use @angular/router-deprecated instead of @angular/router
And use for example this approach www.syntaxsuccess.com/viewarticle/lazy-loading-in-...

R
Roman, 2016-07-12
@uaKorona

WebPack allows you to upload on demand

require.ensure(['jquery'], function(require) {
  require("jquery");
});

I
ITZver, 2017-06-16
@ITZver

I wrote about it on my blog for a long time. Webpack and LazyLoad Angular Modules

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question