-
Book Overview & Buying
-
Table Of Contents
Angular 2 Components
By :
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 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 User {
constructor(name) {
this.name = name;
}
}
export const id = 12345;To use this exported code, we need to import it in another module. We can import this code in various ways:
// import only the function from the module
import { getRandomNumber...