Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • Understanding Modules in Angular2

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 340
    Comment on it

    A module is component in angular that can export classes, function and values. It can work as a library for some other module.
    angular2 library is primary library that is a module and will be imported by some other component.

    This is the code for module in angular2:

    <!DOCTYPE html>
          <title>Angular 2 Modules</title>
          <script src=""></script>
          <script src=""></script>
          <script src=""></script>
          <script src=""></script>
          <script src=""></script>
          <script src=""></script>
          <script src=""></script>
               transpiler: 'typescript',
               typescriptOptions: { emitDecoratorMetadata: true },
               packages: {'app': {defaultExtension: 'ts'}},
               map: { 'app': './angular2/src/app' }
                   .then(null, console.error.bind(console));

    In the above code there are configurations options as follows:

    • index.html file can be configured by typescript version. transpiler option is used to transpile the typescript version to javascript version before application runs. You can have compile errors and warnings if you do not transpile to javascript before running.
    • When the emitDecoratorMetadata option tells that metadata is generated for each class in your code. If this option is not used, unused metadata can affects the file size and application.
    • packages option will have app folder including files with .ts extension.
    • Next main component file is loaded from app folder. It will show error in console if this file is not present.
    • In main.ts file, when bootstrap function is called, it first find metadata then app selector and loads application between app tag.

    You should have these .ts files inside your app folder to run code.


    import {bootstrap} from "angular2/platform/browser"     //importing bootstrap function
    import {MyModulesClass} from "./modules_app.component"  //importing component function

    Next we will make a component and its view in .ts file.


    import {Component, View} from "angular2/core";
    //framework recognizes @Component annotation and knows that we are trying to create a new component
       selector: 'my-app'
      //this template value will be displayed in the browser
      template: '<h2>Welcome</h2>'

    If you run this code, it will show the text which is inside the template option in modules_app.component file.



 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: