Module is a collection of variables, functions, classes and interfaces.
This will be reusable across different other modules with in a project.
Scenario
<>.ts <>.ts | | export class c1 import{c1,c2} from "./module1"; {..} obj=new c1(); export class c2 .. {..} | | [module1] [module2]
<>.ts | import{c1,c2} from "./module1";
....
[module3]
Syntax:
export namespace <namespacename> {
export let <varname>:<type>=value; export function <funcname>() { .. } export class <classname> { .. } export interface <interfacename> { .. } }
->export members are module public, this will be accessible outside module ->export members of one module can be accessed | referenced in another using "import" keyword.
NameSpace:
namespace is a logical container to group related members.
eg: namespace ns1 obj1=new ns1.c1(); | class c1 obj2=new ns2.c1(); {} namespace ns2 | class c1 {} ->namespace is an optional
Now, I have a requirement
module1.ts module2.ts | | namespace inventory --------------------->importing export members of export class category
export class product module1
Module2.tsimport{inventory} from "./Module1";let cateobj=new inventory.category("c001","mobiles");let prodobj=new inventory.product("p001","samsung");cateobj.display();prodobj.display();OutputPS C:\Examples> tsc.cmd Module2.ts PS C:\Examples> node Module2 c001 mobiles p001 samsungNote: Here Module2.ts compilation will perform all the dependent modules[Module1.ts]compilation automatically tsc Module2.ts------> compilation of Module2.ts and Module1.ts | Module2.js Module1.js
Note: Namespace naming is optional
Without Namespace
Output
PS C:\Examples> node Module2 c001 mobiles p001 samsung
Working with multiple Modules Examples
Modify the Module2.ts code as below
No comments:
Post a Comment
Thank you for visiting my blog