Managing dependencies with modules

One of the most important changes introduced in JavaScript is modules. A module is a JavaScript file that gets loaded in a special way. All variables and declarations are scoped to the module. If we like to expose some code to the outside world, we need to export it explicitly. If you try to log the value of this in the top level of the module, you will get undefined.

The export and import statements

The export and import keywords are used to define which part of the code should be exposed to other modules, and which code we will like to import from another module. The following module exposes a function, a class, and a variable:

[user.ts] export function getRandomNumber() { return Math.random(); } export class ...

Get Angular 2 Components now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.