Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Routed components + lazy modules (ocLazyLoad) ng1 #3102

Closed
alo opened this issue Oct 21, 2016 · 3 comments
Closed

Routed components + lazy modules (ocLazyLoad) ng1 #3102

alo opened this issue Oct 21, 2016 · 3 comments
Labels

Comments

@alo
Copy link

alo commented Oct 21, 2016

Hi!

I'm trying to use ui.router(@Version v1.0.0-beta.3) with component routed and oc.lazyLoad(@Version v1.0.9)

This is working

angular.module('app').config(
    ['$stateProvider',
    function ($stateProvider) {

      $stateProvider
        .state('app.sessions', {
          url: '/sessions', 
          template: '<sessions-comp></sessions-comp>', //this is the difference 
          resolve: {
            store: function ($ocLazyLoad) {
              return $ocLazyLoad.load(
                {
                  name: "sessions",
                  files: ["app/dev/sessions/sessions.js",
                    "app/dev/sessions/sessions/sessions.component.js",
                    "app/dev/sessions/shared/sessions.service.js",
                    "app/dev/sessions/shared/sessions-list/sessions-list.component.js"]
                }
              );
            }
          }
        });
      }
    ]
    );

This is NOT working

angular.module('app').config(
    ['$stateProvider',
    function ($stateProvider) {

      $stateProvider
        .state('app.sessions', {
          url: '/sessions', 
          component: 'sessionsComp', //this is the difference 
          resolve: {
            store: function ($ocLazyLoad) {
              return $ocLazyLoad.load(
                {
                  name: "sessions",
                  files: ["app/dev/sessions/sessions.js",
                    "app/dev/sessions/sessions/sessions.component.js",
                    "app/dev/sessions/shared/sessions.service.js",
                    "app/dev/sessions/shared/sessions-list/sessions-list.component.js"]
                }
              );
            }
          }
        });
      }
    ]
    );

I've ask to the ocLazyLoad group Link

Repo to test

Thanks!

@christopherthielen
Copy link
Contributor

Very interesting. I'll take a look.

For now I'll link to this plunker which has a lazy load component hook: http://plnkr.co/edit/3kvrVY?p=preview

@christopherthielen
Copy link
Contributor

christopherthielen commented Oct 21, 2016

OK, I see how this happens now.

The template builder (which is how route-to-component works) requires that the component is available, in order to wire up inputs, etc.

You have two options:

  • Use a transition hook to lazy load code (like the plunker). You can control when the lazy loading occurs. Transition hooks are a better fit for things like lazy loading. In general, use resolve only when you are loading data for a view.
  • Mark your resolve as "EAGER" using a resolve policy. (EAGER resolves are fetched as an onStart hook)
angular.module('myApp', ['ui.router', 'oc.lazyLoad'])
  .config(
  ['$stateProvider', '$urlRouterProvider',
    function ($stateProvider, $urlRouterProvider ) {
      $urlRouterProvider.otherwise('/');
      $stateProvider
      .state('app', {
        url: '/', 
        component: 'appComp',
        resolvePolicy: { deps: { when: "EAGER" } }, // LOAD `deps` RESOLVE EAGERLY
        resolve: {
          deps: ['$ocLazyLoad', function ($ocLazyLoad) {
            return $ocLazyLoad.load(
              {
                name: "lazyModule",
                files: ["./lazy-module.js"]
              }
            );
          }]
        }
      });
    }
  ]
);

@alo
Copy link
Author

alo commented Oct 22, 2016

Thanks so much for the explanation!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants