0

I'm using Angular8 with UI-Router.

Some of my modules are lazy-loaded using UI-Router Future States

export const futureState = {
    name: "app.users.**",
    url: "/users",
    loadChildren: () => import("../user.module").then((m) => m.UserModule)
};

Browsing to /users causes the UserModule to be fetched over the network and loaded. (as part of a file produced by WebPack)

Is it possible for the initial page to trigger the lazy-load the UserModule, so that when /users is visited the UserModule has already been loaded?

1
  • 1
    there is preloading strategy option for such cases in native AngularRouterModule, but you have to switch from UI router Commented Jan 9, 2020 at 14:33

1 Answer 1

1

The following seems to work:

export class HomePageComponent implements OnInit {

    ngOnInit() {
        //Preload the user pages here as it's likely the user will
        //visit them next
        import("./user/user.module").then(m => m.UserModule);
    }
}

I've left the future state definition as above. This causes the UserModule to be loaded:

  • If the user visits the home page
  • If the user goes directly to the user page
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.