{"id":1931,"date":"2022-05-25T09:26:37","date_gmt":"2022-05-25T07:26:37","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=1931"},"modified":"2022-05-25T09:26:37","modified_gmt":"2022-05-25T07:26:37","slug":"javascript-modules-vs-ngmodules","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2022\/05\/25\/javascript-modules-vs-ngmodules\/","title":{"rendered":"JavaScript Modules vs. NgModules"},"content":{"rendered":"\n<p>Modules help organize our code and use it in an efficient way<\/p>\n\n\n\n<p><strong><em>JavaScript modules<\/em><\/strong> and <strong><em>NgModules<\/em><\/strong> can help us modularize our code, but they are very different.However, any Angular application that we create depends on both the ways.<\/p>\n\n\n\n<p>The manner in which the code is organised is different for JavaScript and Angular but the ultimate purpose of a module remains the same.<\/p>\n\n\n\n<h4>JavaScript modules: Files containing code<\/h4>\n\n\n\n<p>A <em><strong>JavaScript-Module <\/strong><\/em>is an individual file with JavaScript code, usually containing a class or a library of functions for a specific purpose within your application.<\/p>\n\n\n\n<p>To make the code in a JavaScript module available to other modules, we use an\u00a0<code>export<\/code>\u00a0statement at the end of the relevant code in the module, such as the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export class AppComponent { \u2026 }<\/code><\/pre>\n\n\n\n<p>Each module has its own top-level scope. In other words, top-level variables and functions in a module are not seen in other scripts or modules. Each module provides a namespace for identifiers to prevent them from clashing with identifiers in other modules.<\/p>\n\n\n\n<p><strong><em>PS:<\/em><\/strong><em><span class=\"has-inline-color has-vivid-cyan-blue-color\"> The Angular framework itself is loaded as a set of <strong>JavaScript modules<\/strong>.<\/span><\/em><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>NgModules: Classes with metadata for compiling<\/h4>\n\n\n\n<p> An\u00a0<a href=\"https:\/\/angular.io\/guide\/glossary#ngmodule\">NgModule<\/a>\u00a0is a class marked by the\u00a0<code>@<a href=\"https:\/\/angular.io\/api\/core\/NgModule\">NgModule<\/a><\/code>\u00a0decorator with a metadata object that describes how that particular part of the application fits together with the other parts. NgModules are specific to Angular.<\/p>\n\n\n\n<p>An <strong><em>NgModule <\/em><\/strong>differs from the JS Modules specifically because unlike JS Modules, NgModules includes the metadata. <em>(with the use of this metadata , we can then import in a Module, another NgModules or even another JavaScript modules.)<\/em> <\/p>\n\n\n\n<p>The\u00a0<code>@<a href=\"https:\/\/angular.io\/api\/core\/NgModule\">NgModule<\/a><\/code>\u00a0metadata plays an important role in guiding the Angular compilation process that converts the application code you write into highly performant JavaScript code. The metadata describes how to compile a component&#8217;s template and how to create an\u00a0<a href=\"https:\/\/angular.io\/guide\/glossary#injector\">injector<\/a>\u00a0at runtime. It identifies the NgModule&#8217;s\u00a0<a href=\"https:\/\/angular.io\/guide\/glossary#component\">components<\/a>,\u00a0<a href=\"https:\/\/angular.io\/guide\/glossary#directive\">directives<\/a>, and\u00a0<a href=\"https:\/\/angular.io\/guide\/glossary#pipe\">pipes<\/a>, and makes some of them public through the\u00a0<code>exports<\/code>\u00a0property so that external components can use them. You can also use an NgModule to add\u00a0<a href=\"https:\/\/angular.io\/guide\/glossary#provider\">providers<\/a>\u00a0for\u00a0<a href=\"https:\/\/angular.io\/guide\/glossary#service\">services<\/a>, so that the services are available elsewhere in your application.<\/p>\n\n\n\n<p><strong><span class=\"has-inline-color has-vivid-red-color\">Another difference with JS Modules : <\/span><\/strong> <em>Rather than defining all member classes in one giant file as a JavaScript module,  we just need to declare the classes (Components\/ directives \/ pipes) belong to the NgModule in the\u00a0<code>@<a href=\"https:\/\/angular.io\/api\/core\/NgModule#declarations\">NgModule.declarations<\/a><\/code>\u00a0list.<\/em><\/p>\n\n\n\n<p>An NgModule can export only the declarable classes it owns or imports from other NgModules. It doesn&#8217;t declare or export any other kind of class<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>An example that uses both<\/h4>\n\n\n\n<p>The root NgModule\u00a0<code>AppModule<\/code>\u00a0generated by the\u00a0<a href=\"https:\/\/angular.io\/cli\">Angular CLI<\/a>\u00a0for a new application project demonstrates how you use both kinds of modules:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"573\" height=\"433\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/05\/image-4.png\" alt=\"\" class=\"wp-image-1932\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/05\/image-4.png 573w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/05\/image-4-300x227.png 300w\" sizes=\"(max-width: 573px) 100vw, 573px\" \/><\/figure><\/div>\n\n\n\n<p>The root NgModule starts with\u00a0<code>import<\/code>\u00a0statements to import JavaScript modules (<em>As we can see it in the first 2 Lines<\/em>). It then configures the\u00a0<code>@<a href=\"https:\/\/angular.io\/api\/core\/NgModule\">NgModule<\/a><\/code>\u00a0with the following arrays:<\/p>\n\n\n\n<ul><li><code><strong>declarations<\/strong><\/code>: The components, directives, and pipes that belong to the NgModule. A new application project&#8217;s root NgModule has only one component, called\u00a0<code>AppComponent<\/code>.<\/li><li><strong><code>imports<\/code>: <\/strong>Other NgModules you are using, so that you can use their declarables. The newly generated root NgModule imports\u00a0<a href=\"https:\/\/angular.io\/api\/platform-browser\/BrowserModule\"><code>BrowserModule<\/code><\/a>\u00a0in order to use browser-specific services such as\u00a0<a href=\"https:\/\/www.w3.org\/TR\/DOM-Level-2-Core\/introduction.html\">DOM<\/a>\u00a0rendering, sanitization, and location.<\/li><li><strong><code>providers<\/code>: <\/strong>Providers of services that components in other NgModules can use. There are no providers in a newly generated root NgModule.<\/li><li><strong><code>bootstrap<\/code>: <\/strong>The\u00a0<a href=\"https:\/\/angular.io\/guide\/entry-components\">entry component<\/a>\u00a0that Angular creates and inserts into the\u00a0<code>index.html<\/code>\u00a0host web page, thereby bootstrapping the application. This entry component,\u00a0<code>AppComponent<\/code>, appears in both the\u00a0<code>declarations<\/code>\u00a0and the\u00a0<code>bootstrap<\/code>\u00a0arrays.<em> (<span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>PS: <\/strong>every Angular Module has it\u00b4s own specific entry component<\/span>. <span class=\"has-inline-color has-vivid-cyan-blue-color\">this is a start-component our Module. It means the component which is first loaded when we call the module <\/span>)  <\/em><\/li><\/ul>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h5>Reference:<\/h5>\n\n\n\n<p><a href=\"https:\/\/angular.io\/\">https:\/\/angular.io\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Modules help organize our code and use it in an efficient way JavaScript modules and NgModules can help us modularize our code, but they are very different.However, any Angular application that we create depends on both the ways. The manner in which the code is organised is different for JavaScript and Angular but the ultimate [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1933,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[37,5],"tags":[40,29,550,552,551,549],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1931"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1931"}],"version-history":[{"count":1,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1931\/revisions"}],"predecessor-version":[{"id":1934,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1931\/revisions\/1934"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1933"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}