Book Image

Getting Started with Angular - Second edition - Second Edition

By : Minko Gechev
Book Image

Getting Started with Angular - Second edition - Second Edition

By: Minko Gechev

Overview of this book

Want to build quick and robust web applications with Angular? This book is the quickest way to get to grips with Angular and take advantage of all its new features.
Table of Contents (16 chapters)
Getting Started with Angular Second Edition
Credits
Foreword
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

ES2015 module loader


The new version of the standard defines a programmatic API to work with modules. This is the so-called module loader API. It allows us to define and import modules, or configure the module loading.

Let's suppose we have the following module definition in the file app.js:

import { square } from './math';
 
export function main() { 
  console.log(square(2)); // 4 
} 

From the init.js file, we can programmatically load the app module and invoke its main function using:

System.import('./app') 
  .then(app => { 
    app.main(); 
  }) 
  .catch(error => { 
    console.log('Terrible error happened', error); 
  }); 

The global object System has a method called import that allows us to import modules using their identifier. In the preceding snippet, we import the module app defined in app.js. System.import returns a promise that could be resolved on success or rejected in case of an error. Once the promise is resolved...