简体   繁体   中英

Build Private Modules in Angular2

I tried creating my own module and import it like in app module below.

This code below is fine but I wanted to show an example and then propose my question below.

Suppose my folder structure is like below:- `

├── app
│   ├── app.component.ts
│   └── app.module.ts
├── credit-card
│   ├── credit-card-mask.pipe.ts
│   ├── credit-card.component.ts
│   ├── credit-card.module.ts
│   └── credit-card.service.ts
├── index.html
└── main.ts

`

My credit card module is like below:-

 import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { CreditCardMaskPipe } from './credit-card-mask.pipe'; import { CreditCardService } from './credit-card.service'; import { CreditCardComponent } from './credit-card.component'; @NgModule({ imports: [CommonModule], declarations: [ CreditCardMaskPipe, CreditCardComponent ], providers: [CreditCardService], exports: [CreditCardComponent] }) export class CreditCardModule {} 

My app module is below:-

 import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { CreditCardModule } from '../credit-card/credit-card.module'; import { AppComponent } from './app.component'; @NgModule({ imports: [ BrowserModule, CreditCardModule ], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule { } 

I tried to search for creating private modules in Angular2 over the internet but got no relevant information.

Unlike the module above I need to now implement a private module in my application.

Suppose I want my credit card module to be private so by removing the export word before class CreditCardModule will be enough to make my module private?

Let me know if I am right or wrong. Or is there any other way so my application can have private modules in angular2 app?

Also, I referred the page https://angular.io/guide/ngmodule-faq

What should I not export?

Don't export the following:

  • Private components, directives, and pipes that you need only within components declared in this module. If you don't want another module to see it, don't export it.
  • Non-declarable objects such as services, functions, configurations, and entity models.
  • Components that are only loaded dynamically by the router or by bootstrapping. Such entry components can never be selected in another component's template. While there's no harm in exporting them, there's also no benefit.
  • Pure service modules that don't have public (exported) declarations. For example, there's no point in re-exporting HttpModule because it doesn't export anything. It's only purpose is to add http service providers to the application as a whole.

But I am not clear.

Thanks in advance.

I strongly suggest reading this article:

Here is the quote from it:

Angular introduces the concept of module encapsulation in a way similar to ES modules. It basically means that declarable types — components, directives and pipes — can only be used by components declared inside this module.

What's important to understand is that there's no encapsulation for services, so you don't import or export them.

Unlike the module above I need to now implement a private module in my application.

There's no such thing as a private module. However, as explained above you can make declarable types private to that module by simply not exporting them. For example:

AComponent is public since it's exported

@NgModule({
  ...
  declarations: [AComponent],
  exports: [AComponent]           
})
export class AModule { }

AComponent is private since it's not exported

@NgModule({
  ...
  declarations: [AComponent]         
})
export class AModule { }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM