Question about loading modules with requireJS and Angularjs

Asked

Viewed 369 times

2

I’m studying a code from a former employee of the company and he used requireJS + Angularjs. He has created several modules with plenty of services etc, but my question is: when using a service or a directive, do require and angular load the whole module or just the called service or directive? Because I understood the advantages of using require, but I didn’t understand how it works in practice.

  • You can put the code piece here?

3 answers

1

Module loading depends on how the application is configured.

As the very initial documentation suggests:

  1. Developing the Loader first charges the main and then each module at the time required.
  2. After development, on the Internet, loading each module individually would completely kill the performance of a non-trivial application. Therefore it is common to package the modules in a single larger script and add it preemptively from the page, thus the Loader identifies that modules are already present and executes scripts quickly.

Packaging scripts to optimize page loading is a tricky art. You can read a little more about this in this my other answer.

0

To make sense think of the following:

An application with multiple modules, multiple controllers, directives, services, etc..

If you don’t use require, you need to include all the files at once in your view, using Requirejs, you can:

Separating each module, service, controller, directive into separate files, into their respective directories, they will be loaded only when necessary.

In addition, your application becomes more organized, as you can put together a file structure that is easily serviceable. And its elements can be reused when necessary.

0

Complementing with the reply of @Pedro Laini I am also using a dynamic load, through the ocLazyLoad.

It can be used in conjunction with your route system, being loaded from a resolve. So let’s say you have an administrative area with a permissions system, it can further assist in the security of your application, since the module will only be loaded if you pass the resolve.

In addition, as already said by Pedro Laini, as the modules will only be loaded as needed, their initial load will be much lower, giving more speed to your app.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.