Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NgModule vs Component

Tags:

So I'm learning angular2 now and reading ngbook2 about the modules. Modules contain components, but also can import different modules with their public components.

And the question is: What is the scope of the module component (in this meaning scope as parts of an application, not the reach of variables inside). Is module the whole application or just some part like header, containing its components?

What is the typicaly used convention?

like image 799
mjanisz1 Avatar asked Oct 04 '16 20:10

mjanisz1


People also ask

What is the difference between NgModule and component?

This root NgModule is what's used to bootstrap the Angular application. It is in this root module that we also bootstrap the root-level component. This root-level component is the application's main view, which hosts other components for the application. An NgModule is a class adorned with the @NgModule decorator.

What is an NgModule?

NgModules configure the injector and the compiler and help organize related things together. An NgModule is a class marked by the @NgModule decorator. @NgModule takes a metadata object that describes how to compile a component's template and how to create an injector at runtime.

Why would you export from NgModule?

An export what you put is the exports property of the @NgModule decorator. It enables an Angular module to expose some of its components/directives/pipes to the other modules in the applications. Without it, the components/directives/pipes defined in a module could only be used in that module.


2 Answers

A common way is to see a module as a distinct feature implementation, that can consist of zero or more services, components, directives, and pipes and import modules that are used to implement that feature.

A module can define what of its content it exports to be made available for importers.

An application can consist of one or more modules with the root module importing zero or more modules where each imported module can import zero or more modules.

Modules can also be used to - split an app into parts that can be lazy loaded - reused in different applications - to create a naming scope for selectors.

A component is a reusable visual building block for the user interface that is rendered as HTML with event handlers to react on user actions and model data changes.

like image 176
Günter Zöchbauer Avatar answered Oct 31 '22 16:10

Günter Zöchbauer


In one whole application, there can be one main Module. But your application can be divided into multiple modules and can be injected into the Main Module. Let's say you have a big application library which contains movies, trailer, songs etc. You gave the main app Module name 'MyApp'. but further you divided your app into multiple components say.

  • MovieComponent
  • SongComponent (further divided into AudioComponent and VideoComponent)
  • TrailerComponent

All these Component developed and tested individually and integrated to our MyAppModule.

In case anything happens in our Trailer part we can discard/test our TrailerComponent.

So It is easy to maintain a large Applications.

I hope this will help you to understand. any suggestions most welcomed

like image 38
UniCoder Avatar answered Oct 31 '22 18:10

UniCoder