Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Angular export empty class in app.modules.ts?

I am new in Angular, I am using Angular 4, and I made an app using the Angular CLI, by ng new command.

In main.ts, we have

 ...
import {AppModule} from './app/app.module';
 .
 .
platformBrowserDynamic().bootstrapModule(AppModule);

and AppModule is defined (as you see) in app/app.module, here is what we have in app.module.ts:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

As you see it says export class AppModule { }, and it also used in platformBrowserDynamic().bootstrapModule(AppModule);

Can someone please explain this for me?

like image 679
TypeScript Learner Avatar asked Jun 01 '17 20:06

TypeScript Learner


People also ask

Why do we export class in Angular?

Angular imports/exports are used to make the content of one module available to be used in another module. Allows you to use the directives from FormsModule and AuthRoutingModule in AuthModule and registers the services provided by these modules in the AppModule scope or the closed lazy-loaded root scope.

What is index TS file in Angular?

index. ts help us to keep all related thing together and we don't need to worry about the source file name. We can import all thing by using source folder name. import { getName, getAnyThing } from './util'; Here util is folder name not file name which has index.

What is app Module TS in Angular?

Tell Angular how to construct and bootstrap the app in the root "AppModule". An Angular module class describes how the application parts fit together. Every application has at least one Angular module, the root module that you bootstrap to launch the application.

Can you export a module Angular?

You can export any declarable class —components, directives, and pipes— whether it's declared in this NgModule or in an imported NgModule. You can re-export entire imported NgModules, which effectively re-export all of their exported classes. An NgModule can even export a module that it doesn't import.


1 Answers

The body of the class is indeed empty. But that decorator above the class (@NgModule) is giving that class its functionality. So really, that class isn't empty. It just doesn't require any extra logic after the decorator is applied to it. bootstrapModule takes a class as input and assumes that that class is decorated with @NgModule configured in a manner similar to what you have (declarations, imports, providers, etc.).

like image 109
Jake Smith Avatar answered Nov 02 '22 18:11

Jake Smith