Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Import and export statements specific to Angular 2?

Tags:

angular

Are these statements from a specific language/framework, or something custom to Angular 2. I'm trying to understand how Angular 2 works.

import {} from ''
export class ... { }
like image 733
rubixibuc Avatar asked Jan 30 '16 21:01

rubixibuc


1 Answers

No, the import and export keywords are not Angular2 specific.

import

The import statement is used to import functions, objects or primitives that have been exported from an external module, another script, etc. Basically we use the import module to load the Angular2 bundles. For example:

'import {Component, and many more here....} from angular2/core`

Here, Component is a module which is being imported from the bundles of Angular2. angular2/core is not a path - rather, it is predefined bundle of Angular2. If you look at the source code of angular bundle, you can see there's System.register("angular2/core", .... - for this, we have already imported system.js before our Angular in the index.html file. system.js is the module loader here.

The import feature is not implemented in any browsers natively at this time. It is implemented in many transpilers, such as the Traceur Compiler, Babel or Rollup. For more information about the import module, you can read this tutorial about the import module. For the list of imports for Angular2 you can read about this here.

export

As it is clear from the name, the export statement is used to export functions, objects or primitives from a given file (or module). export is just an identifier so that it can be imported via import in another script. export is also an ES6 module like the import module. An article about export is here. As suggested by Eric in the comments, we have used the export class in the Angular2 project to export classes of the component. For an example, you can refer to my repo here.

like image 113
Pardeep Jain Avatar answered Oct 20 '22 00:10

Pardeep Jain